TYPO3 CMS  TYPO3_8-7
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 
23 {
27  protected $supportedOptions = [
28  'minimum' => [0, 'Minimum length for a valid string', 'integer'],
29  'maximum' => [PHP_INT_MAX, 'Maximum length for a valid string', 'integer']
30  ];
31 
41  public function isValid($value)
42  {
43  if ($this->options['maximum'] < $this->options['minimum']) {
44  throw new \TYPO3\CMS\Extbase\Validation\Exception\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  $stringLength = mb_strlen($value, 'utf-8');
58  $isValid = true;
59  if ($stringLength < $this->options['minimum']) {
60  $isValid = false;
61  }
62  if ($stringLength > $this->options['maximum']) {
63  $isValid = false;
64  }
65 
66  if ($isValid === false) {
67  if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) {
68  $this->addError(
69  $this->translateErrorMessage(
70  'validator.stringlength.between',
71  'extbase',
72  [
73  $this->options['minimum'],
74  $this->options['maximum']
75  ]
76  ),
77  1428504122,
78  [$this->options['minimum'], $this->options['maximum']]
79  );
80  } elseif ($this->options['minimum'] > 0) {
81  $this->addError(
82  $this->translateErrorMessage(
83  'validator.stringlength.less',
84  'extbase',
85  [
86  $this->options['minimum']
87  ]
88  ),
89  1238108068,
90  [$this->options['minimum']]
91  );
92  } else {
93  $this->addError(
94  $this->translateErrorMessage(
95  'validator.stringlength.exceed',
96  'extbase',
97  [
98  $this->options['maximum']
99  ]
100  ),
101  1238108069,
102  [$this->options['maximum']]
103  );
104  }
105  }
106  }
107 }
addError($message, $code, array $arguments=[], $title='')
translateErrorMessage($translateKey, $extensionName, $arguments=[])