16664e7cdd2a6a10076fed514111ee2ed3b3bd5d
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / BetweenValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Between rule
29 * Value must be between the min and max. inclusively optional
30 *
31 * @author Patrick Broens <patrick@patrickbroens.nl>
32 */
33 class BetweenValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator implements \TYPO3\CMS\Form\Validation\ValidatorInterface {
34
35 /**
36 * Minimum value
37 *
38 * @var mixed
39 */
40 protected $minimum;
41
42 /**
43 * Maximum value
44 *
45 * @var mixed
46 */
47 protected $maximum;
48
49 /**
50 * If TRUE, minimum and maximum values are included in comparison
51 *
52 * @var boolean
53 */
54 protected $inclusive;
55
56 /**
57 * Constructor
58 *
59 * @param array $arguments Typoscript configuration
60 */
61 public function __construct($arguments) {
62 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum'])->setInclusive($arguments['inclusive']);
63 parent::__construct($arguments);
64 }
65
66 /**
67 * Returns TRUE if submitted value validates according to rule
68 *
69 * @return boolean
70 * @see \TYPO3\CMS\Form\Validation\ValidatorInterface::isValid()
71 */
72 public function isValid() {
73 if ($this->requestHandler->has($this->fieldName)) {
74 $value = $this->requestHandler->getByMethod($this->fieldName);
75 if ($this->inclusive) {
76 if ($value < $this->minimum || $value > $this->maximum) {
77 return FALSE;
78 }
79 } else {
80 if ($value <= $this->minimum || $value >= $this->maximum) {
81 return FALSE;
82 }
83 }
84 }
85 return TRUE;
86 }
87
88 /**
89 * Set the minimum value
90 *
91 * @param mixed $minimum Minimum value
92 * @return object Rule object
93 */
94 public function setMinimum($minimum) {
95 $this->minimum = $minimum;
96 return $this;
97 }
98
99 /**
100 * Set the maximum value
101 *
102 * @param mixed $maximum Maximum value
103 * @return object Rule object
104 */
105 public function setMaximum($maximum) {
106 $this->maximum = $maximum;
107 return $this;
108 }
109
110 /**
111 * Set boolean to make minimum and maximum value inclusive in comparison
112 *
113 * @param boolean $inclusive True is inclusive
114 * @return object Rule object
115 */
116 public function setInclusive($inclusive) {
117 if ($inclusive === NULL) {
118 $this->inclusive = FALSE;
119 } else {
120 $this->inclusive = (bool) $inclusive;
121 }
122 return $this;
123 }
124
125 /**
126 * Get the local language label(s) for the message
127 * Overrides the abstract
128 *
129 * @param string $type The type
130 * @return string The local language message label
131 * @see \TYPO3\CMS\Form\Validation\AbstractValidator::_getLocalLanguageLabel()
132 */
133 protected function getLocalLanguageLabel($type) {
134 $label = strtolower(get_class($this)) . '.' . $type;
135 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
136 if ($this->inclusive) {
137 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
138 }
139 $message = implode(', ', $messages);
140 return $message;
141 }
142
143 /**
144 * Substitute makers in the message text
145 * Overrides the abstract
146 *
147 * @param string $message Message text with markers
148 * @return string Message text with substituted markers
149 */
150 protected function substituteValues($message) {
151 $message = str_replace('%minimum', $this->minimum, $message);
152 $message = str_replace('%maximum', $this->maximum, $message);
153 return $message;
154 }
155
156 }