11b1d2bbc8a914c1394ab4a0899d37bdb301efc3
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / LengthValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008 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 * Length rule
28 *
29 * @author Patrick Broens <patrick@patrickbroens.nl>
30 * @package TYPO3
31 * @subpackage form
32 */
33 class LengthValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
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 * Constructor
51 *
52 * @param array $arguments Typoscript configuration
53 * @return void
54 */
55 public function __construct($arguments) {
56 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum']);
57 parent::__construct($arguments);
58 }
59
60 /**
61 * Returns TRUE if submitted value validates according to rule
62 *
63 * @return boolean
64 * @see tx_form_System_Validate_Interface::isValid()
65 */
66 public function isValid() {
67 if ($this->requestHandler->has($this->fieldName)) {
68 $value = $this->requestHandler->getByMethod($this->fieldName);
69 $length = iconv_strlen($value);
70 if ($length < $this->minimum) {
71 return FALSE;
72 }
73 if ($this->maximum !== NULL && $length > $this->maximum) {
74 return FALSE;
75 }
76 }
77 return TRUE;
78 }
79
80 /**
81 * Set the minimum value
82 *
83 * @param integer $minimum Minimum value
84 * @return object Rule object
85 */
86 public function setMinimum($minimum) {
87 $this->minimum = (int) $minimum;
88 return $this;
89 }
90
91 /**
92 * Set the maximum value
93 *
94 * @param integer $maximum Maximum value
95 * @return object Rule object
96 */
97 public function setMaximum($maximum) {
98 if (empty($maximum)) {
99 $this->maximum = NULL;
100 } else {
101 $this->maximum = (int) $maximum;
102 }
103 return $this;
104 }
105
106 /**
107 * Get the local language label(s) for the message
108 * Overrides the abstract
109 *
110 * @param string $type The type
111 * @return string The local language message label
112 * @see tx_form_System_Validate_Abstract::_getLocalLanguageLabel()
113 */
114 protected function getLocalLanguageLabel($type) {
115 $label = strtolower(get_class($this)) . '.' . $type;
116 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
117 if ($this->maximum !== NULL) {
118 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
119 }
120 $message = implode(', ', $messages);
121 return $message;
122 }
123
124 /**
125 * Substitute makers in the message text
126 * Overrides the abstract
127 *
128 * @param string $message Message text with markers
129 * @return string Message text with substituted markers
130 */
131 protected function substituteValues($message) {
132 $message = str_replace('%minimum', $this->minimum, $message);
133 $message = str_replace('%maximum', $this->maximum, $message);
134 return $message;
135 }
136
137 }
138
139
140 ?>