TYPO3 CMS  TYPO3_7-6
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 
42  public function isValid($value)
43  {
44  if ($this->options['maximum'] < $this->options['minimum']) {
45  throw new \TYPO3\CMS\Extbase\Validation\Exception\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  // @todo Use \TYPO3\CMS\Core\Charset\CharsetConverter::strlen() instead; How do we get the charset?
59  $stringLength = strlen($value);
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(
71  $this->translateErrorMessage(
72  'validator.stringlength.between',
73  'extbase',
74  [
75  $this->options['minimum'],
76  $this->options['maximum']
77  ]
78  ), 1428504122, [$this->options['minimum'], $this->options['maximum']]);
79  } elseif ($this->options['minimum'] > 0) {
80  $this->addError(
81  $this->translateErrorMessage(
82  'validator.stringlength.less',
83  'extbase',
84  [
85  $this->options['minimum']
86  ]
87  ), 1238108068, [$this->options['minimum']]);
88  } else {
89  $this->addError(
90  $this->translateErrorMessage(
91  'validator.stringlength.exceed',
92  'extbase',
93  [
94  $this->options['maximum']
95  ]
96  ), 1238108069, [$this->options['maximum']]);
97  }
98  }
99  }
100 }
addError($message, $code, array $arguments=[], $title='')
translateErrorMessage($translateKey, $extensionName, $arguments=[])