‪TYPO3CMS  ‪main
StringLengthValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
26 {
30  protected ‪$supportedOptions = [
31  'minimum' => [0, 'Minimum length for a valid string', 'integer'],
32  'maximum' => [PHP_INT_MAX, 'Maximum length for a valid string', 'integer'],
33  ];
34 
42  public function ‪isValid(mixed $value): void
43  {
44  if ($this->options['maximum'] < $this->options['minimum']) {
45  throw new ‪InvalidValidationOptionsException('The \'maximum\' is shorter than the \'minimum\' in the StringLengthValidator.', 1238107096);
46  }
47 
48  if (is_object($value)) {
49  if (!method_exists($value, '__toString')) {
50  $this->‪addError('The given object could not be converted to a string.', 1238110957);
51  return;
52  }
53  } elseif (!is_string($value)) {
54  $this->‪addError('The given value was not a valid string.', 1269883975);
55  return;
56  }
57 
58  $value = (string)$value;
59  $stringLength = mb_strlen($value, 'utf-8');
60  $isValid = true;
61  if ($stringLength < $this->options['minimum']) {
62  $isValid = false;
63  }
64  if ($stringLength > $this->options['maximum']) {
65  $isValid = false;
66  }
67 
68  if ($isValid === false) {
69  if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) {
70  $this->‪addError(
72  'validator.stringlength.between',
73  'extbase',
74  [
75  $this->options['minimum'],
76  $this->options['maximum'],
77  ]
78  ),
79  1428504122,
80  [$this->options['minimum'], $this->options['maximum']]
81  );
82  } elseif ($this->options['minimum'] > 0) {
83  $this->‪addError(
85  'validator.stringlength.less',
86  'extbase',
87  [
88  $this->options['minimum'],
89  ]
90  ),
91  1238108068,
92  [$this->options['minimum']]
93  );
94  } else {
95  $this->‪addError(
97  'validator.stringlength.exceed',
98  'extbase',
99  [
100  $this->options['maximum'],
101  ]
102  ),
103  1238108069,
104  [$this->options['maximum']]
105  );
106  }
107  }
108  }
109 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator\$supportedOptions
‪array $supportedOptions
Definition: StringLengthValidator.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError(string $message, int $code, array $arguments=[], string $title='')
Definition: AbstractValidator.php:82
‪TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
Definition: StringLengthValidator.php:26
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪translateErrorMessage(string $translateKey, string $extensionName, array $arguments=[])
Definition: AbstractValidator.php:122
‪TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator\isValid
‪isValid(mixed $value)
Definition: StringLengthValidator.php:41
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:18
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:25