[BUGFIX] Make ValidatorResolver respect namespaces
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / ValidatorResolver.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Validator resolver to automatically find a appropriate validator for a given subject
32 */
33 class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * Match validator names and options
37 *
38 * @var string
39 */
40 const PATTERN_MATCH_VALIDATORS = '/
41 (?:^|,\\s*)
42 (?P<validatorName>[a-z0-9_:.\\\\]+)
43 \\s*
44 (?:\\(
45 (?P<validatorOptions>(?:\\s*[a-z0-9]+\\s*=\\s*(?:
46 "(?:\\\\"|[^"])*"
47 |\'(?:\\\\\'|[^\'])*\'
48 |(?:\\s|[^,"\']*)
49 )(?:\\s|,)*)*)
50 \\))?
51 /ixS';
52 /**
53 * Match validator options (to parse actual options)
54 *
55 * @var string
56 */
57 const PATTERN_MATCH_VALIDATOROPTIONS = '/
58 \\s*
59 (?P<optionName>[a-z0-9]+)
60 \\s*=\\s*
61 (?P<optionValue>
62 "(?:\\\\"|[^"])*"
63 |\'(?:\\\\\'|[^\'])*\'
64 |(?:\\s|[^,"\']*)
65 )
66 /ixS';
67 /**
68 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
69 */
70 protected $objectManager;
71
72 /**
73 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
74 */
75 protected $reflectionService;
76
77 /**
78 * @var array
79 */
80 protected $baseValidatorConjunctions = array();
81
82 /**
83 * Injects the object manager
84 *
85 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
86 * @return void
87 */
88 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
89 $this->objectManager = $objectManager;
90 }
91
92 /**
93 * Injects the reflection service
94 *
95 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
96 * @return void
97 */
98 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
99 $this->reflectionService = $reflectionService;
100 }
101
102 /**
103 * Get a validator for a given data type. Returns a validator implementing
104 * the \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface or NULL if no validator
105 * could be resolved.
106 *
107 * @param string $validatorName Either one of the built-in data types or fully qualified validator class name
108 * @param array $validatorOptions Options to be passed to the validator
109 * @return \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface Validator or NULL if none found.
110 */
111 public function createValidator($validatorName, array $validatorOptions = array()) {
112 $validatorClassName = $this->resolveValidatorObjectName($validatorName);
113 if ($validatorClassName === FALSE) {
114 return NULL;
115 }
116 $validator = $this->objectManager->get($validatorClassName, $validatorOptions);
117 if (!$validator instanceof \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface) {
118 return NULL;
119 }
120 if (method_exists($validator, 'setOptions')) {
121 // @deprecated since Extbase 1.4.0, will be removed in Extbase 6.1
122 $validator->setOptions($validatorOptions);
123 }
124 return $validator;
125 }
126
127 /**
128 * Resolves and returns the base validator conjunction for the given data type.
129 *
130 * If no validator could be resolved (which usually means that no validation is necessary),
131 * NULL is returned.
132 *
133 * @param string $dataType The data type to search a validator for. Usually the fully qualified object name
134 * @return \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator The validator conjunction or NULL
135 */
136 public function getBaseValidatorConjunction($dataType) {
137 if (!isset($this->baseValidatorConjunctions[$dataType])) {
138 $this->baseValidatorConjunctions[$dataType] = $this->buildBaseValidatorConjunction($dataType);
139 }
140 return $this->baseValidatorConjunctions[$dataType];
141 }
142
143 /**
144 * Detects and registers any validators for arguments:
145 * - by the data type specified in the
146 *
147 * @param string $className
148 * @param string $methodName
149 * @throws Exception\NoSuchValidatorException
150 * @throws Exception\InvalidValidationConfigurationException
151 * @return array An Array of ValidatorConjunctions for each method parameters.
152 */
153 public function buildMethodArgumentsValidatorConjunctions($className, $methodName) {
154 $validatorConjunctions = array();
155 $methodParameters = $this->reflectionService->getMethodParameters($className, $methodName);
156 $methodTagsValues = $this->reflectionService->getMethodTagsValues($className, $methodName);
157 if (!count($methodParameters)) {
158 // early return in case no parameters were found.
159 return $validatorConjunctions;
160 }
161 foreach ($methodParameters as $parameterName => $methodParameter) {
162 $validatorConjunction = $this->createValidator('Conjunction');
163 $typeValidator = $this->createValidator($methodParameter['type']);
164 if ($typeValidator !== NULL) {
165 $validatorConjunction->addValidator($typeValidator);
166 }
167 $validatorConjunctions[$parameterName] = $validatorConjunction;
168 }
169 if (isset($methodTagsValues['validate'])) {
170 foreach ($methodTagsValues['validate'] as $validateValue) {
171 $parsedAnnotation = $this->parseValidatorAnnotation($validateValue);
172 foreach ($parsedAnnotation['validators'] as $validatorConfiguration) {
173 $newValidator = $this->createValidator($validatorConfiguration['validatorName'], $validatorConfiguration['validatorOptions']);
174 if ($newValidator === NULL) {
175 throw new \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Could not resolve class name for validator "' . $validatorConfiguration['validatorName'] . '".', 1239853109);
176 }
177 if (isset($validatorConjunctions[$parsedAnnotation['argumentName']])) {
178 $validatorConjunctions[$parsedAnnotation['argumentName']]->addValidator($newValidator);
179 } else {
180 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Validator specified for argument name "' . $parsedAnnotation['argumentName'] . '", but this argument does not exist.', 1253172726);
181 }
182 }
183 }
184 }
185 return $validatorConjunctions;
186 }
187
188 /**
189 * Builds a base validator conjunction for the given data type.
190 *
191 * The base validation rules are those which were declared directly in a class (typically
192 * a model) through some @validate annotations on properties.
193 *
194 * Additionally, if a custom validator was defined for the class in question, it will be added
195 * to the end of the conjunction. A custom validator is found if it follows the naming convention
196 * "Replace '\Model\' by '\Validator\' and append "Validator".
197 *
198 * Example: $dataType is F3\Foo\Domain\Model\Quux, then the Validator will be found if it has the
199 * name F3\Foo\Domain\Validator\QuuxValidator
200 *
201 * @param string $dataType The data type to build the validation conjunction for. Needs to be the fully qualified object name.
202 * @throws Exception\NoSuchValidatorException
203 * @return \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator The validator conjunction or NULL
204 */
205 protected function buildBaseValidatorConjunction($dataType) {
206 $validatorConjunction = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator');
207 // Model based validator
208 if (class_exists($dataType)) {
209 $validatorCount = 0;
210 $objectValidator = $this->createValidator('GenericObject');
211 foreach ($this->reflectionService->getClassPropertyNames($dataType) as $classPropertyName) {
212 $classPropertyTagsValues = $this->reflectionService->getPropertyTagsValues($dataType, $classPropertyName);
213 if (!isset($classPropertyTagsValues['validate'])) {
214 continue;
215 }
216 foreach ($classPropertyTagsValues['validate'] as $validateValue) {
217 $parsedAnnotation = $this->parseValidatorAnnotation($validateValue);
218 foreach ($parsedAnnotation['validators'] as $validatorConfiguration) {
219 $newValidator = $this->createValidator($validatorConfiguration['validatorName'], $validatorConfiguration['validatorOptions']);
220 if ($newValidator === NULL) {
221 throw new \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException('Invalid validate annotation in ' . $dataType . '::' . $classPropertyName . ': Could not resolve class name for validator "' . $validatorConfiguration['validatorName'] . '".', 1241098027);
222 }
223 $objectValidator->addPropertyValidator($classPropertyName, $newValidator);
224 $validatorCount++;
225 }
226 }
227 }
228 if ($validatorCount > 0) {
229 $validatorConjunction->addValidator($objectValidator);
230 }
231 }
232 // Custom validator for the class
233 $possibleValidatorClassName = str_replace(array('_Model_', '\\Model\\'), array('_Validator_','\\Validator\\'), $dataType) . 'Validator';
234 $customValidator = $this->createValidator($possibleValidatorClassName);
235 if ($customValidator !== NULL) {
236 $validatorConjunction->addValidator($customValidator);
237 }
238 return $validatorConjunction;
239 }
240
241 /**
242 * Parses the validator options given in @validate annotations.
243 *
244 * @param string $validateValue
245 * @return array
246 */
247 protected function parseValidatorAnnotation($validateValue) {
248 $matches = array();
249 if ($validateValue[0] === '$') {
250 $parts = explode(' ', $validateValue, 2);
251 $validatorConfiguration = array('argumentName' => ltrim($parts[0], '$'), 'validators' => array());
252 preg_match_all(self::PATTERN_MATCH_VALIDATORS, $parts[1], $matches, PREG_SET_ORDER);
253 } else {
254 $validatorConfiguration = array('validators' => array());
255 preg_match_all(self::PATTERN_MATCH_VALIDATORS, $validateValue, $matches, PREG_SET_ORDER);
256 }
257 foreach ($matches as $match) {
258 $validatorOptions = array();
259 if (isset($match['validatorOptions'])) {
260 $validatorOptions = $this->parseValidatorOptions($match['validatorOptions']);
261 }
262 $validatorConfiguration['validators'][] = array('validatorName' => $match['validatorName'], 'validatorOptions' => $validatorOptions);
263 }
264 return $validatorConfiguration;
265 }
266
267 /**
268 * Parses $rawValidatorOptions not containing quoted option values.
269 * $rawValidatorOptions will be an empty string afterwards (pass by ref!).
270 *
271 * @param string &$rawValidatorOptions
272 * @return array An array of optionName/optionValue pairs
273 */
274 protected function parseValidatorOptions($rawValidatorOptions) {
275 $validatorOptions = array();
276 $parsedValidatorOptions = array();
277 preg_match_all(self::PATTERN_MATCH_VALIDATOROPTIONS, $rawValidatorOptions, $validatorOptions, PREG_SET_ORDER);
278 foreach ($validatorOptions as $validatorOption) {
279 $parsedValidatorOptions[trim($validatorOption['optionName'])] = trim($validatorOption['optionValue']);
280 }
281 array_walk($parsedValidatorOptions, array($this, 'unquoteString'));
282 return $parsedValidatorOptions;
283 }
284
285 /**
286 * Removes escapings from a given argument string and trims the outermost
287 * quotes.
288 *
289 * This method is meant as a helper for regular expression results.
290 *
291 * @param string &$quotedValue Value to unquote
292 * @return void
293 */
294 protected function unquoteString(&$quotedValue) {
295 switch ($quotedValue[0]) {
296 case '"':
297 $quotedValue = str_replace('\\"', '"', trim($quotedValue, '"'));
298 break;
299 case '\'':
300 $quotedValue = str_replace('\\\'', '\'', trim($quotedValue, '\''));
301 break;
302 }
303 $quotedValue = str_replace('\\\\', '\\', $quotedValue);
304 }
305
306 /**
307 * Returns an object of an appropriate validator for the given class. If no validator is available
308 * FALSE is returned
309 *
310 * @param string $validatorName Either the fully qualified class name of the validator or the short name of a built-in validator
311 * @return string|boolean Name of the validator object or FALSE
312 */
313 protected function resolveValidatorObjectName($validatorName) {
314 if (strpbrk($validatorName, '_\\') !== FALSE && class_exists($validatorName)) {
315 return $validatorName;
316 }
317 list($extensionName, $extensionValidatorName) = explode(':', $validatorName);
318 if (empty($extensionValidatorName)) {
319 $possibleClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\' . $this->unifyDataType($validatorName) . 'Validator';
320 } else {
321 if (strpos($extensionName, '.') !== FALSE) {
322 $extensionNameParts = explode('.', $extensionName);
323 $extensionName = array_pop($extensionNameParts);
324 $vendorName = implode('\\', $extensionNameParts);
325 $possibleClassName = $vendorName . '\\' . $extensionName . '\\Validation\\Validator\\' . $extensionValidatorName . 'Validator';
326 } else {
327 $possibleClassName = 'Tx_' . $extensionName . '_Validation_Validator_' . $extensionValidatorName . 'Validator';
328 }
329 }
330 if (class_exists($possibleClassName)) {
331 return $possibleClassName;
332 }
333 return FALSE;
334 }
335
336 /**
337 * Preprocess data types. Used to map primitive PHP types to DataTypes used in Extbase.
338 *
339 * @param string $type Data type to unify
340 * @return string unified data type
341 */
342 protected function unifyDataType($type) {
343 switch ($type) {
344 case 'int':
345 $type = 'Integer';
346 break;
347 case 'bool':
348 $type = 'Boolean';
349 break;
350 case 'double':
351 $type = 'Float';
352 break;
353 case 'numeric':
354 $type = 'Number';
355 break;
356 case 'mixed':
357 $type = 'Raw';
358 break;
359 }
360 return ucfirst($type);
361 }
362
363 }
364 ?>