[CLEANUP] Add missing empty lines before classes' closing curly braces
[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 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 // @todo Use \TYPO3\CMS\Core\Charset\CharsetConverter::strlen() instead; How do we get the charset?
58 $stringLength = strlen($value);
59 $isValid = TRUE;
60 if ($stringLength < $this->options['minimum']) {
61 $isValid = FALSE;
62 }
63 if ($stringLength > $this->options['maximum']) {
64 $isValid = FALSE;
65 }
66
67 if ($isValid === FALSE) {
68 if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) {
69 $this->addError(
70 $this->translateErrorMessage(
71 'validator.stringlength.between',
72 'extbase',
73 array (
74 $this->options['minimum'],
75 $this->options['maximum']
76 )
77 ), 1238108067, array($this->options['minimum'], $this->options['maximum']));
78 } elseif ($this->options['minimum'] > 0) {
79 $this->addError(
80 $this->translateErrorMessage(
81 'validator.stringlength.less',
82 'extbase',
83 array(
84 $this->options['minimum']
85 )
86 ), 1238108068, array($this->options['minimum']));
87 } else {
88 $this->addError(
89 $this->translateErrorMessage(
90 'validator.stringlength.exceed',
91 'extbase',
92 array(
93 $this->options['maximum']
94 )
95 ), 1238108069, array($this->options['maximum']));
96 }
97 }
98 }
99
100 }