[TASK] Remove closing PHP tags
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / StringLengthValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 /**
32 * Validator for string length.
33 *
34 * @api
35 */
36 class StringLengthValidator extends AbstractValidator {
37
38 /**
39 * @var array
40 */
41 protected $supportedOptions = array(
42 'minimum' => array(0, 'Minimum length for a valid string', 'integer'),
43 'maximum' => array(PHP_INT_MAX, 'Maximum length for a valid string', 'integer')
44 );
45
46 /**
47 * Checks if the given value is a valid string (or can be cast to a string
48 * if an object is given) and its length is between minimum and maximum
49 * specified in the validation options.
50 *
51 * @param mixed $value The value that should be validated
52 * @return void
53 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
54 * @api
55 */
56 public function isValid($value) {
57 if ($this->options['maximum'] < $this->options['minimum']) {
58 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('The \'maximum\' is shorter than the \'minimum\' in the StringLengthValidator.', 1238107096);
59 }
60
61 if (is_object($value)) {
62 if (!method_exists($value, '__toString')) {
63 $this->addError('The given object could not be converted to a string.', 1238110957);
64 return;
65 }
66 } elseif (!is_string($value)) {
67 $this->addError('The given value was not a valid string.', 1269883975);
68 return;
69 }
70
71 // TODO Use \TYPO3\CMS\Core\Charset\CharsetConverter::strlen() instead; How do we get the charset?
72 $stringLength = strlen($value);
73 $isValid = TRUE;
74 if ($stringLength < $this->options['minimum']) {
75 $isValid = FALSE;
76 }
77 if ($stringLength > $this->options['maximum']) {
78 $isValid = FALSE;
79 }
80
81 if ($isValid === FALSE) {
82 if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) {
83 $this->addError(
84 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
85 'validator.stringlength.between',
86 'extbase',
87 array (
88 $this->options['minimum'],
89 $this->options['maximum']
90 )
91 ), 1238108067, array($this->options['minimum'], $this->options['maximum']));
92 } elseif ($this->options['minimum'] > 0) {
93 $this->addError(
94 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
95 'validator.stringlength.less',
96 'extbase',
97 array(
98 $this->options['minimum']
99 )
100 ), 1238108068, array($this->options['minimum']));
101 } else {
102 $this->addError(
103 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
104 'validator.stringlength.exceed',
105 'extbase',
106 array(
107 $this->options['maximum']
108 )
109 ), 1238108069, array($this->options['maximum']));
110 }
111 }
112 }
113 }