[TASK] Merge submodule extbase into core
[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 * Validator for string length
32 */
33 class StringLengthValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
34
35 /**
36 * Returns TRUE, if the given property ($value) is a valid string and its length
37 * is between 'minimum' (defaults to 0 if not specified) and 'maximum' (defaults to infinite if not specified)
38 * to be specified in the validation options.
39 *
40 * If at least one error occurred, the result is FALSE.
41 *
42 * @param mixed $value The value that should be validated
43 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidSubjectException
44 * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
45 * @return boolean TRUE if the value is valid, FALSE if an error occured
46 */
47 public function isValid($value) {
48 $this->errors = array();
49 if (isset($this->options['minimum']) && isset($this->options['maximum']) && $this->options['maximum'] < $this->options['minimum']) {
50 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('The \'maximum\' is shorter than the \'minimum\' in the StringLengthValidator.', 1238107096);
51 }
52 if (is_object($value) && !method_exists($value, '__toString')) {
53 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidSubjectException('The given object could not be converted to a string.', 1238110957);
54 }
55 // TODO Use \TYPO3\CMS\Core\Charset\CharsetConverter::strlen() instead; How do we get the charset?
56 $stringLength = strlen($value);
57 $isValid = TRUE;
58 if (isset($this->options['minimum']) && $stringLength < $this->options['minimum']) {
59 $isValid = FALSE;
60 }
61 if (isset($this->options['maximum']) && $stringLength > $this->options['maximum']) {
62 $isValid = FALSE;
63 }
64 if ($isValid === FALSE) {
65 if (isset($this->options['minimum']) && isset($this->options['maximum'])) {
66 $this->addError(
67 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
68 'validator.stringlength.between',
69 'extbase',
70 array (
71 $this->options['minimum'],
72 $this->options['maximum']
73 )
74 ), 1238108067, array($this->options['minimum'], $this->options['maximum']));
75 } elseif (isset($this->options['minimum'])) {
76 $this->addError(
77 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
78 'validator.stringlength.less',
79 'extbase',
80 array(
81 $this->options['minimum']
82 )
83 ), 1238108068, array($this->options['minimum']));
84 } else {
85 $this->addError(
86 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
87 'validator.stringlength.exceed',
88 'extbase',
89 array(
90 $this->options['maximum']
91 )
92 ), 1238108069, array($this->options['maximum']));
93 }
94 }
95 return $isValid;
96 }
97 }
98
99 ?>