[TASK] Make TYPO3 Core PSR-2 standard compliant
[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 = array(
28 'minimum' => array(0, 'Minimum length for a valid string', 'integer'),
29 'maximum' => array(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 * @return void
39 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
40 * @api
41 */
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 array(
75 $this->options['minimum'],
76 $this->options['maximum']
77 )
78 ), 1428504122, array($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 array(
85 $this->options['minimum']
86 )
87 ), 1238108068, array($this->options['minimum']));
88 } else {
89 $this->addError(
90 $this->translateErrorMessage(
91 'validator.stringlength.exceed',
92 'extbase',
93 array(
94 $this->options['maximum']
95 )
96 ), 1238108069, array($this->options['maximum']));
97 }
98 }
99 }
100 }