[TASK] Streamline return tags in phpdocs
[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 * @api
21 */
22 class StringLengthValidator extends AbstractValidator
23 {
24 /**
25 * @var array
26 */
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
32 /**
33 * Checks if the given value is a valid string (or can be cast to a string
34 * if an object is given) and its length is between minimum and maximum
35 * specified in the validation options.
36 *
37 * @param mixed $value The value that should be validated
38 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
39 * @api
40 */
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 ), 1428504122, [$this->options['minimum'], $this->options['maximum']]);
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 ), 1238108068, [$this->options['minimum']]);
86 } else {
87 $this->addError(
88 $this->translateErrorMessage(
89 'validator.stringlength.exceed',
90 'extbase',
91 [
92 $this->options['maximum']
93 ]
94 ), 1238108069, [$this->options['maximum']]);
95 }
96 }
97 }
98 }