[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / StringLengthValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
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
17 /**
18 * Validator for string length.
19 */
20 class StringLengthValidator extends AbstractValidator
21 {
22 /**
23 * @var array
24 */
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
30 /**
31 * Checks if the given value is a valid string (or can be cast to a string
32 * if an object is given) and its length is between minimum and maximum
33 * specified in the validation options.
34 *
35 * @param mixed $value The value that should be validated
36 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
37 */
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(
66 $this->translateErrorMessage(
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(
79 $this->translateErrorMessage(
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(
91 $this->translateErrorMessage(
92 'validator.stringlength.exceed',
93 'extbase',
94 [
95 $this->options['maximum']
96 ]
97 ),
98 1238108069,
99 [$this->options['maximum']]
100 );
101 }
102 }
103 }
104 }