[BUGFIX] Call LOCALISATION_OBJECT_NAME 'static'ally
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / LengthValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
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 * Length rule
19 *
20 * @author Patrick Broens <patrick@patrickbroens.nl>
21 */
22 class LengthValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
23
24 /**
25 * Constant for localisation
26 *
27 * @var string
28 */
29 const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_length';
30
31 /**
32 * Minimum value
33 *
34 * @var mixed
35 */
36 protected $minimum;
37
38 /**
39 * Maximum value
40 *
41 * @var mixed
42 */
43 protected $maximum;
44
45 /**
46 * TYPO3 charset encoding object
47 *
48 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
49 */
50 protected $charsetConverter = NULL;
51
52 /**
53 * Constructor
54 *
55 * @param array $arguments Typoscript configuration
56 */
57 public function __construct($arguments) {
58 $this->charsetConverter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
59 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum']);
60 parent::__construct($arguments);
61 }
62
63 /**
64 * Returns TRUE if submitted value validates according to rule
65 *
66 * @return boolean
67 * @see \TYPO3\CMS\Form\Validation\ValidatorInterface::isValid()
68 */
69 public function isValid() {
70 if ($this->requestHandler->has($this->fieldName)) {
71 $value = $this->requestHandler->getByMethod($this->fieldName);
72 $length = $this->charsetConverter->strlen('utf-8', $value);
73 if ($length < $this->minimum) {
74 return FALSE;
75 }
76 if ($this->maximum !== NULL && $length > $this->maximum) {
77 return FALSE;
78 }
79 }
80 return TRUE;
81 }
82
83 /**
84 * Set the minimum value
85 *
86 * @param integer $minimum Minimum value
87 * @return object Rule object
88 */
89 public function setMinimum($minimum) {
90 $this->minimum = (int)$minimum;
91 return $this;
92 }
93
94 /**
95 * Set the maximum value
96 *
97 * @param integer $maximum Maximum value
98 * @return object Rule object
99 */
100 public function setMaximum($maximum) {
101 if (empty($maximum)) {
102 $this->maximum = NULL;
103 } else {
104 $this->maximum = (int)$maximum;
105 }
106 return $this;
107 }
108
109 /**
110 * Get the local language label(s) for the message
111 * Overrides the abstract
112 *
113 * @param string $type The type
114 * @return string The local language message label
115 * @see \TYPO3\CMS\Form\Validation\AbstractValidator::_getLocalLanguageLabel()
116 */
117 protected function getLocalLanguageLabel($type) {
118 $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
119 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
120 if ($this->maximum !== NULL) {
121 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
122 }
123 $message = implode(', ', $messages);
124 return $message;
125 }
126
127 /**
128 * Substitute makers in the message text
129 * Overrides the abstract
130 *
131 * @param string $message Message text with markers
132 * @return string Message text with substituted markers
133 */
134 protected function substituteValues($message) {
135 return str_replace(
136 array('%minimum', '%maximum'),
137 array($this->minimum, $this->maximum),
138 $message
139 );
140 }
141
142 }