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