[BUGFIX] Remove dependency to iconv lib in form
[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 * TYPO3 charset encoding object
51 *
52 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
53 */
54 protected $charsetConverter = NULL;
55
56 /**
57 * Constructor
58 *
59 * @param array $arguments Typoscript configuration
60 * @return void
61 */
62 public function __construct($arguments) {
63 $this->charsetConverter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
64 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum']);
65 parent::__construct($arguments);
66 }
67
68 /**
69 * Returns TRUE if submitted value validates according to rule
70 *
71 * @return boolean
72 * @see tx_form_System_Validate_Interface::isValid()
73 */
74 public function isValid() {
75 if ($this->requestHandler->has($this->fieldName)) {
76 $value = $this->requestHandler->getByMethod($this->fieldName);
77 $length = $this->charsetConverter->strlen('utf-8', $value);
78 if ($length < $this->minimum) {
79 return FALSE;
80 }
81 if ($this->maximum !== NULL && $length > $this->maximum) {
82 return FALSE;
83 }
84 }
85 return TRUE;
86 }
87
88 /**
89 * Set the minimum value
90 *
91 * @param integer $minimum Minimum value
92 * @return object Rule object
93 */
94 public function setMinimum($minimum) {
95 $this->minimum = (int) $minimum;
96 return $this;
97 }
98
99 /**
100 * Set the maximum value
101 *
102 * @param integer $maximum Maximum value
103 * @return object Rule object
104 */
105 public function setMaximum($maximum) {
106 if (empty($maximum)) {
107 $this->maximum = NULL;
108 } else {
109 $this->maximum = (int) $maximum;
110 }
111 return $this;
112 }
113
114 /**
115 * Get the local language label(s) for the message
116 * Overrides the abstract
117 *
118 * @param string $type The type
119 * @return string The local language message label
120 * @see tx_form_System_Validate_Abstract::_getLocalLanguageLabel()
121 */
122 protected function getLocalLanguageLabel($type) {
123 $label = strtolower(get_class($this)) . '.' . $type;
124 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
125 if ($this->maximum !== NULL) {
126 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
127 }
128 $message = implode(', ', $messages);
129 return $message;
130 }
131
132 /**
133 * Substitute makers in the message text
134 * Overrides the abstract
135 *
136 * @param string $message Message text with markers
137 * @return string Message text with substituted markers
138 */
139 protected function substituteValues($message) {
140 $message = str_replace('%minimum', $this->minimum, $message);
141 $message = str_replace('%maximum', $this->maximum, $message);
142 return $message;
143 }
144
145 }
146
147
148 ?>