[TASK] PHP7 compliance - part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / BetweenValidator.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 * Between rule
19 * Value must be between the min and max. inclusively optional
20 */
21 class BetweenValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator implements \TYPO3\CMS\Form\Validation\ValidatorInterface {
22
23 /**
24 * Constant for localisation
25 *
26 * @var string
27 */
28 const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_between';
29
30 /**
31 * Minimum value
32 *
33 * @var mixed
34 */
35 protected $minimum;
36
37 /**
38 * Maximum value
39 *
40 * @var mixed
41 */
42 protected $maximum;
43
44 /**
45 * If TRUE, minimum and maximum values are included in comparison
46 *
47 * @var bool
48 */
49 protected $inclusive;
50
51 /**
52 * Constructor
53 *
54 * @param array $arguments Typoscript configuration
55 */
56 public function __construct($arguments) {
57 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum'])->setInclusive($arguments['inclusive']);
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 if ($this->inclusive) {
71 if ($value < $this->minimum || $value > $this->maximum) {
72 return FALSE;
73 }
74 } else {
75 if ($value <= $this->minimum || $value >= $this->maximum) {
76 return FALSE;
77 }
78 }
79 }
80 return TRUE;
81 }
82
83 /**
84 * Set the minimum value
85 *
86 * @param mixed $minimum Minimum value
87 * @return BetweenValidator Rule object
88 */
89 public function setMinimum($minimum) {
90 $this->minimum = $minimum;
91 return $this;
92 }
93
94 /**
95 * Set the maximum value
96 *
97 * @param mixed $maximum Maximum value
98 * @return BetweenValidator Rule object
99 */
100 public function setMaximum($maximum) {
101 $this->maximum = $maximum;
102 return $this;
103 }
104
105 /**
106 * Set boolean to make minimum and maximum value inclusive in comparison
107 *
108 * @param bool $inclusive True is inclusive
109 * @return BetweenValidator Rule object
110 */
111 public function setInclusive($inclusive) {
112 if ($inclusive === NULL) {
113 $this->inclusive = FALSE;
114 } else {
115 $this->inclusive = (bool)$inclusive;
116 }
117 return $this;
118 }
119
120 /**
121 * Get the local language label(s) for the message
122 * Overrides the abstract
123 *
124 * @param string $type The type
125 * @return string The local language message label
126 * @see \TYPO3\CMS\Form\Validation\AbstractValidator::_getLocalLanguageLabel()
127 */
128 protected function getLocalLanguageLabel($type = '') {
129 $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
130 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
131 if ($this->inclusive) {
132 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
133 }
134 $message = implode(', ', $messages);
135 return $message;
136 }
137
138 /**
139 * Substitute makers in the message text
140 * Overrides the abstract
141 *
142 * @param string $message Message text with markers
143 * @return string Message text with substituted markers
144 */
145 protected function substituteValues($message) {
146 return str_replace(
147 array('%minimum', '%maximum'),
148 array($this->minimum, $this->maximum),
149 $message
150 );
151 }
152
153 }