[TASK] Make preperations for backporting Flow validation api
[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 * Returns TRUE, if the given property ($value) is a valid string and its length
40 * is between 'minimum' (defaults to 0 if not specified) and 'maximum' (defaults to infinite if not specified)
41 * to be specified in the validation options.
42 *
43 * If at least one error occurred, the result is FALSE.
44 *
45 * @param mixed $value The value that should be validated
46 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidSubjectException
47 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
48 * @return boolean TRUE if the value is valid, FALSE if an error occured
49 */
50 public function isValid($value) {
51 $this->errors = array();
52 if (isset($this->options['minimum']) && isset($this->options['maximum']) && $this->options['maximum'] < $this->options['minimum']) {
53 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('The \'maximum\' is shorter than the \'minimum\' in the StringLengthValidator.', 1238107096);
54 }
55 if (is_object($value) && !method_exists($value, '__toString')) {
56 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidSubjectException('The given object could not be converted to a string.', 1238110957);
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 (isset($this->options['minimum']) && $stringLength < $this->options['minimum']) {
62 $isValid = FALSE;
63 }
64 if (isset($this->options['maximum']) && $stringLength > $this->options['maximum']) {
65 $isValid = FALSE;
66 }
67 if ($isValid === FALSE) {
68 if (isset($this->options['minimum']) && isset($this->options['maximum'])) {
69 $this->addError(
70 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
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 (isset($this->options['minimum'])) {
79 $this->addError(
80 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
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 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
90 'validator.stringlength.exceed',
91 'extbase',
92 array(
93 $this->options['maximum']
94 )
95 ), 1238108069, array($this->options['maximum']));
96 }
97 }
98 return $isValid;
99 }
100 }
101
102 ?>