[TASK] PHP7 compliance - part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / AbstractValidator.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Request;
19
20 /**
21 * Abstract for validation
22 */
23 abstract class AbstractValidator implements \TYPO3\CMS\Form\Validation\ValidatorInterface {
24
25 /**
26 * The name of the field for the rule
27 *
28 * @var string
29 */
30 protected $fieldName;
31
32 /**
33 * Message of the rule as cObj
34 * This variable will contain two keys:
35 * $message['cObj] = TEXT
36 * $message['cObj.] = array('value', 'This is the message')
37 *
38 * @var array
39 */
40 protected $message = array();
41
42 /**
43 * Possible error message after validation as cObj
44 * This variable will contain two keys:
45 * $message['cObj] = TEXT
46 * $message['cObj.] = array('value', 'This is the error')
47 *
48 * @var array
49 */
50 protected $error = array();
51
52 /**
53 * Display the message like mandatory
54 *
55 * @var bool
56 */
57 protected $showMessage = TRUE;
58
59 /**
60 * Localization handler object
61 *
62 * @var \TYPO3\CMS\Form\Localization
63 */
64 protected $localizationHandler;
65
66 /**
67 * Request handler object
68 *
69 * @var Request
70 */
71 protected $requestHandler;
72
73 /**
74 * The content object
75 *
76 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
77 */
78 protected $localCobj;
79
80 /**
81 * Inject the Request Handler
82 *
83 * @param Request $requestHandler
84 * @return void
85 */
86 public function injectRequestHandler(Request $requestHandler) {
87 $this->requestHandler = $requestHandler;
88 }
89
90 /**
91 * Constructor
92 *
93 * @param array $arguments Typoscript configuration for the validation rule
94 */
95 public function __construct($arguments) {
96 $this->localCobj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
97 $this->injectRequestHandler(GeneralUtility::makeInstance(Request::class));
98 $this->localizationHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Localization::class);
99 $this->setFieldName($arguments['element']);
100 $this->setMessage($arguments['message.'], $arguments['message']);
101 $this->setShowMessage($arguments['showMessage']);
102 $this->setError($arguments['error.'], $arguments['error']);
103 }
104
105 /**
106 * Set the fieldName
107 *
108 * @param string $fieldName The field name
109 * @return ValidatorInterface The rule object
110 */
111 public function setFieldName($fieldName) {
112 $this->fieldName = (string)$fieldName;
113 return $this;
114 }
115
116 /**
117 * Returns the field name
118 *
119 * @return string The field name
120 */
121 public function getFieldName() {
122 return $this->fieldName;
123 }
124
125 /**
126 * Returns the message
127 *
128 * @return array Typoscript for cObj
129 */
130 public function getMessage() {
131 return $this->message;
132 }
133
134 /**
135 * Returns the error
136 *
137 * @return array Typoscript for cObj
138 */
139 public function getError() {
140 return $this->error;
141 }
142
143 /**
144 * Set the message, like 'required' for the validation rule
145 * and substitutes markers for values, like %maximum
146 *
147 * The output will be a Typoscript array to use as cObj
148 * If no parameter is given, it will take the default locallang label
149 * If only first parameter, then it's supposed to be a TEXT cObj
150 * When both are filled, it's supposed to be a cObj made by the administrator
151 * In the last case, no markers will be substituted
152 *
153 * @param string|array $message Message as string or TS
154 * @param string $type Name of the cObj
155 * @return void
156 */
157 public function setMessage($message = '', $type = 'TEXT') {
158 if (empty($message)) {
159 if (!empty($type)) {
160 $message = $type;
161 $type = 'TEXT';
162 } else {
163 $type = 'TEXT';
164 $message = $this->getLocalLanguageLabel('message');
165 }
166 $value['value'] = $this->substituteValues($message);
167 } elseif (!is_array($message)) {
168 $value['value'] = $this->substituteValues($message);
169 } else {
170 $value = $message;
171 }
172 $this->message['cObj'] = (string)$type;
173 $this->message['cObj.'] = $value;
174 }
175
176 /**
177 * Set the error, like 'This field is required' for the validation rule
178 *
179 * The output will be a Typoscript array to use as cObj
180 * If no parameter is given, it will take the default locallang label
181 * If only first parameter, then it's supposed to be a TEXT cObj
182 * When both are filled, it's supposed to be a cObj made by the administrator
183 * In the last case, no markers will be substituted
184 *
185 * @param mixed $error Message as string or TS
186 * @param string $type Name of the cObj
187 * @return void
188 */
189 public function setError($error = '', $type = 'TEXT') {
190 if (empty($error)) {
191 if (!empty($type)) {
192 $error = $type;
193 $type = 'TEXT';
194 } else {
195 $type = 'TEXT';
196 $error = $this->getLocalLanguageLabel('error');
197 }
198 $value['value'] = $this->substituteValues($error);
199 } elseif (!is_array($error)) {
200 $value['value'] = $this->substituteValues($error);
201 } else {
202 $value = $error;
203 }
204 $this->error['cObj'] = (string)$type;
205 $this->error['cObj.'] = $value;
206 }
207
208 /**
209 * Set if message needs to be displayed
210 *
211 * @param bool $show TRUE is display
212 * @return ValidatorInterface The rule object
213 */
214 public function setShowMessage($show) {
215 if ($show === NULL) {
216 $this->showMessage = TRUE;
217 } else {
218 $this->showMessage = (bool)$show;
219 }
220 return $this;
221 }
222
223 /**
224 * Returns TRUE when message needs to be displayed
225 *
226 * @return bool
227 */
228 public function messageMustBeDisplayed() {
229 return $this->showMessage;
230 }
231
232 /**
233 * Substitute makers in the message text
234 * In some cases this method will be override by rule class
235 *
236 * @param string $message Message text with markers
237 * @return string Message text with substituted markers
238 */
239 protected function substituteValues($message) {
240 return $message;
241 }
242
243 /**
244 * Get the local language label(s) for the message
245 * In some cases this method will be override by rule class
246 *
247 * @param string $type The type
248 * @return string The local language message label
249 */
250 protected function getLocalLanguageLabel($type = '') {
251 $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
252 $message = $this->localizationHandler->getLocalLanguageLabel($label);
253 return $message;
254 }
255
256 }