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