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