[TASK] Use arrays in str_replace() calls
[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-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 * Length rule
29 *
30 * @author Patrick Broens <patrick@patrickbroens.nl>
31 */
32 class LengthValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
33
34 /**
35 * Minimum value
36 *
37 * @var mixed
38 */
39 protected $minimum;
40
41 /**
42 * Maximum value
43 *
44 * @var mixed
45 */
46 protected $maximum;
47
48 /**
49 * TYPO3 charset encoding object
50 *
51 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
52 */
53 protected $charsetConverter = NULL;
54
55 /**
56 * Constructor
57 *
58 * @param array $arguments Typoscript configuration
59 */
60 public function __construct($arguments) {
61 $this->charsetConverter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
62 $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum']);
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 $length = $this->charsetConverter->strlen('utf-8', $value);
76 if ($length < $this->minimum) {
77 return FALSE;
78 }
79 if ($this->maximum !== NULL && $length > $this->maximum) {
80 return FALSE;
81 }
82 }
83 return TRUE;
84 }
85
86 /**
87 * Set the minimum value
88 *
89 * @param integer $minimum Minimum value
90 * @return object Rule object
91 */
92 public function setMinimum($minimum) {
93 $this->minimum = (int) $minimum;
94 return $this;
95 }
96
97 /**
98 * Set the maximum value
99 *
100 * @param integer $maximum Maximum value
101 * @return object Rule object
102 */
103 public function setMaximum($maximum) {
104 if (empty($maximum)) {
105 $this->maximum = NULL;
106 } else {
107 $this->maximum = (int) $maximum;
108 }
109 return $this;
110 }
111
112 /**
113 * Get the local language label(s) for the message
114 * Overrides the abstract
115 *
116 * @param string $type The type
117 * @return string The local language message label
118 * @see \TYPO3\CMS\Form\Validation\AbstractValidator::_getLocalLanguageLabel()
119 */
120 protected function getLocalLanguageLabel($type) {
121 $label = strtolower(get_class($this)) . '.' . $type;
122 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
123 if ($this->maximum !== NULL) {
124 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
125 }
126 $message = implode(', ', $messages);
127 return $message;
128 }
129
130 /**
131 * Substitute makers in the message text
132 * Overrides the abstract
133 *
134 * @param string $message Message text with markers
135 * @return string Message text with substituted markers
136 */
137 protected function substituteValues($message) {
138 return str_replace(
139 array('%minimum', '%maximum'),
140 array($this->minimum, $this->maximum),
141 $message
142 );
143 }
144
145 }