Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Validator;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@4
|
15 use Symfony\Component\Validator\Constraints\Composite;
|
Chris@0
|
16 use Symfony\Component\Validator\Constraints\GroupSequence;
|
Chris@0
|
17 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
Chris@0
|
18 use Symfony\Component\Validator\Context\ExecutionContext;
|
Chris@0
|
19 use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
Chris@0
|
20 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
21 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
|
Chris@0
|
22 use Symfony\Component\Validator\Exception\RuntimeException;
|
Chris@0
|
23 use Symfony\Component\Validator\Exception\UnsupportedMetadataException;
|
Chris@0
|
24 use Symfony\Component\Validator\Exception\ValidatorException;
|
Chris@0
|
25 use Symfony\Component\Validator\Mapping\CascadingStrategy;
|
Chris@0
|
26 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
|
Chris@4
|
27 use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
|
Chris@0
|
28 use Symfony\Component\Validator\Mapping\GenericMetadata;
|
Chris@0
|
29 use Symfony\Component\Validator\Mapping\MetadataInterface;
|
Chris@0
|
30 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
|
Chris@0
|
31 use Symfony\Component\Validator\Mapping\TraversalStrategy;
|
Chris@0
|
32 use Symfony\Component\Validator\ObjectInitializerInterface;
|
Chris@0
|
33 use Symfony\Component\Validator\Util\PropertyPath;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Recursive implementation of {@link ContextualValidatorInterface}.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
39 */
|
Chris@0
|
40 class RecursiveContextualValidator implements ContextualValidatorInterface
|
Chris@0
|
41 {
|
Chris@0
|
42 private $context;
|
Chris@0
|
43 private $defaultPropertyPath;
|
Chris@0
|
44 private $defaultGroups;
|
Chris@0
|
45 private $metadataFactory;
|
Chris@0
|
46 private $validatorFactory;
|
Chris@0
|
47 private $objectInitializers;
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Creates a validator for the given context.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @param ExecutionContextInterface $context The execution context
|
Chris@0
|
53 * @param MetadataFactoryInterface $metadataFactory The factory for
|
Chris@0
|
54 * fetching the metadata
|
Chris@0
|
55 * of validated objects
|
Chris@0
|
56 * @param ConstraintValidatorFactoryInterface $validatorFactory The factory for creating
|
Chris@0
|
57 * constraint validators
|
Chris@0
|
58 * @param ObjectInitializerInterface[] $objectInitializers The object initializers
|
Chris@0
|
59 */
|
Chris@4
|
60 public function __construct(ExecutionContextInterface $context, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = [])
|
Chris@0
|
61 {
|
Chris@0
|
62 $this->context = $context;
|
Chris@0
|
63 $this->defaultPropertyPath = $context->getPropertyPath();
|
Chris@4
|
64 $this->defaultGroups = [$context->getGroup() ?: Constraint::DEFAULT_GROUP];
|
Chris@0
|
65 $this->metadataFactory = $metadataFactory;
|
Chris@0
|
66 $this->validatorFactory = $validatorFactory;
|
Chris@0
|
67 $this->objectInitializers = $objectInitializers;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public function atPath($path)
|
Chris@0
|
74 {
|
Chris@0
|
75 $this->defaultPropertyPath = $this->context->getPropertyPath($path);
|
Chris@0
|
76
|
Chris@0
|
77 return $this;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * {@inheritdoc}
|
Chris@0
|
82 */
|
Chris@0
|
83 public function validate($value, $constraints = null, $groups = null)
|
Chris@0
|
84 {
|
Chris@0
|
85 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
Chris@0
|
86
|
Chris@0
|
87 $previousValue = $this->context->getValue();
|
Chris@0
|
88 $previousObject = $this->context->getObject();
|
Chris@0
|
89 $previousMetadata = $this->context->getMetadata();
|
Chris@0
|
90 $previousPath = $this->context->getPropertyPath();
|
Chris@0
|
91 $previousGroup = $this->context->getGroup();
|
Chris@0
|
92 $previousConstraint = null;
|
Chris@0
|
93
|
Chris@0
|
94 if ($this->context instanceof ExecutionContext || method_exists($this->context, 'getConstraint')) {
|
Chris@0
|
95 $previousConstraint = $this->context->getConstraint();
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 // If explicit constraints are passed, validate the value against
|
Chris@0
|
99 // those constraints
|
Chris@0
|
100 if (null !== $constraints) {
|
Chris@0
|
101 // You can pass a single constraint or an array of constraints
|
Chris@0
|
102 // Make sure to deal with an array in the rest of the code
|
Chris@4
|
103 if (!\is_array($constraints)) {
|
Chris@4
|
104 $constraints = [$constraints];
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 $metadata = new GenericMetadata();
|
Chris@0
|
108 $metadata->addConstraints($constraints);
|
Chris@0
|
109
|
Chris@0
|
110 $this->validateGenericNode(
|
Chris@0
|
111 $value,
|
Chris@0
|
112 $previousObject,
|
Chris@4
|
113 \is_object($value) ? spl_object_hash($value) : null,
|
Chris@0
|
114 $metadata,
|
Chris@0
|
115 $this->defaultPropertyPath,
|
Chris@0
|
116 $groups,
|
Chris@0
|
117 null,
|
Chris@0
|
118 TraversalStrategy::IMPLICIT,
|
Chris@0
|
119 $this->context
|
Chris@0
|
120 );
|
Chris@0
|
121
|
Chris@0
|
122 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
|
Chris@0
|
123 $this->context->setGroup($previousGroup);
|
Chris@0
|
124
|
Chris@0
|
125 if (null !== $previousConstraint) {
|
Chris@0
|
126 $this->context->setConstraint($previousConstraint);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 return $this;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 // If an object is passed without explicit constraints, validate that
|
Chris@0
|
133 // object against the constraints defined for the object's class
|
Chris@4
|
134 if (\is_object($value)) {
|
Chris@0
|
135 $this->validateObject(
|
Chris@0
|
136 $value,
|
Chris@0
|
137 $this->defaultPropertyPath,
|
Chris@0
|
138 $groups,
|
Chris@0
|
139 TraversalStrategy::IMPLICIT,
|
Chris@0
|
140 $this->context
|
Chris@0
|
141 );
|
Chris@0
|
142
|
Chris@0
|
143 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
|
Chris@0
|
144 $this->context->setGroup($previousGroup);
|
Chris@0
|
145
|
Chris@0
|
146 return $this;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 // If an array is passed without explicit constraints, validate each
|
Chris@0
|
150 // object in the array
|
Chris@4
|
151 if (\is_array($value)) {
|
Chris@0
|
152 $this->validateEachObjectIn(
|
Chris@0
|
153 $value,
|
Chris@0
|
154 $this->defaultPropertyPath,
|
Chris@0
|
155 $groups,
|
Chris@0
|
156 $this->context
|
Chris@0
|
157 );
|
Chris@0
|
158
|
Chris@0
|
159 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
|
Chris@0
|
160 $this->context->setGroup($previousGroup);
|
Chris@0
|
161
|
Chris@0
|
162 return $this;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@4
|
165 throw new RuntimeException(sprintf('Cannot validate values of type "%s" automatically. Please provide a constraint.', \gettype($value)));
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * {@inheritdoc}
|
Chris@0
|
170 */
|
Chris@0
|
171 public function validateProperty($object, $propertyName, $groups = null)
|
Chris@0
|
172 {
|
Chris@0
|
173 $classMetadata = $this->metadataFactory->getMetadataFor($object);
|
Chris@0
|
174
|
Chris@0
|
175 if (!$classMetadata instanceof ClassMetadataInterface) {
|
Chris@4
|
176 throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
Chris@0
|
180 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
Chris@0
|
181 $cacheKey = spl_object_hash($object);
|
Chris@0
|
182 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
|
Chris@0
|
183
|
Chris@0
|
184 $previousValue = $this->context->getValue();
|
Chris@0
|
185 $previousObject = $this->context->getObject();
|
Chris@0
|
186 $previousMetadata = $this->context->getMetadata();
|
Chris@0
|
187 $previousPath = $this->context->getPropertyPath();
|
Chris@0
|
188 $previousGroup = $this->context->getGroup();
|
Chris@0
|
189
|
Chris@0
|
190 foreach ($propertyMetadatas as $propertyMetadata) {
|
Chris@0
|
191 $propertyValue = $propertyMetadata->getPropertyValue($object);
|
Chris@0
|
192
|
Chris@0
|
193 $this->validateGenericNode(
|
Chris@0
|
194 $propertyValue,
|
Chris@0
|
195 $object,
|
Chris@4
|
196 $cacheKey.':'.\get_class($object).':'.$propertyName,
|
Chris@0
|
197 $propertyMetadata,
|
Chris@0
|
198 $propertyPath,
|
Chris@0
|
199 $groups,
|
Chris@0
|
200 null,
|
Chris@0
|
201 TraversalStrategy::IMPLICIT,
|
Chris@0
|
202 $this->context
|
Chris@0
|
203 );
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
|
Chris@0
|
207 $this->context->setGroup($previousGroup);
|
Chris@0
|
208
|
Chris@0
|
209 return $this;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
|
Chris@0
|
216 {
|
Chris@0
|
217 $classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass);
|
Chris@0
|
218
|
Chris@0
|
219 if (!$classMetadata instanceof ClassMetadataInterface) {
|
Chris@4
|
220 throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
Chris@0
|
224 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
Chris@0
|
225
|
Chris@4
|
226 if (\is_object($objectOrClass)) {
|
Chris@0
|
227 $object = $objectOrClass;
|
Chris@4
|
228 $class = \get_class($object);
|
Chris@0
|
229 $cacheKey = spl_object_hash($objectOrClass);
|
Chris@0
|
230 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
|
Chris@0
|
231 } else {
|
Chris@0
|
232 // $objectOrClass contains a class name
|
Chris@0
|
233 $object = null;
|
Chris@0
|
234 $class = $objectOrClass;
|
Chris@0
|
235 $cacheKey = null;
|
Chris@0
|
236 $propertyPath = $this->defaultPropertyPath;
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 $previousValue = $this->context->getValue();
|
Chris@0
|
240 $previousObject = $this->context->getObject();
|
Chris@0
|
241 $previousMetadata = $this->context->getMetadata();
|
Chris@0
|
242 $previousPath = $this->context->getPropertyPath();
|
Chris@0
|
243 $previousGroup = $this->context->getGroup();
|
Chris@0
|
244
|
Chris@0
|
245 foreach ($propertyMetadatas as $propertyMetadata) {
|
Chris@0
|
246 $this->validateGenericNode(
|
Chris@0
|
247 $value,
|
Chris@0
|
248 $object,
|
Chris@0
|
249 $cacheKey.':'.$class.':'.$propertyName,
|
Chris@0
|
250 $propertyMetadata,
|
Chris@0
|
251 $propertyPath,
|
Chris@0
|
252 $groups,
|
Chris@0
|
253 null,
|
Chris@0
|
254 TraversalStrategy::IMPLICIT,
|
Chris@0
|
255 $this->context
|
Chris@0
|
256 );
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
|
Chris@0
|
260 $this->context->setGroup($previousGroup);
|
Chris@0
|
261
|
Chris@0
|
262 return $this;
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * {@inheritdoc}
|
Chris@0
|
267 */
|
Chris@0
|
268 public function getViolations()
|
Chris@0
|
269 {
|
Chris@0
|
270 return $this->context->getViolations();
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 /**
|
Chris@0
|
274 * Normalizes the given group or list of groups to an array.
|
Chris@0
|
275 *
|
Chris@4
|
276 * @param string|GroupSequence|(string|GroupSequence)[] $groups The groups to normalize
|
Chris@0
|
277 *
|
Chris@4
|
278 * @return (string|GroupSequence)[] A group array
|
Chris@0
|
279 */
|
Chris@0
|
280 protected function normalizeGroups($groups)
|
Chris@0
|
281 {
|
Chris@4
|
282 if (\is_array($groups)) {
|
Chris@0
|
283 return $groups;
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@4
|
286 return [$groups];
|
Chris@0
|
287 }
|
Chris@0
|
288
|
Chris@0
|
289 /**
|
Chris@0
|
290 * Validates an object against the constraints defined for its class.
|
Chris@0
|
291 *
|
Chris@0
|
292 * If no metadata is available for the class, but the class is an instance
|
Chris@0
|
293 * of {@link \Traversable} and the selected traversal strategy allows
|
Chris@0
|
294 * traversal, the object will be iterated and each nested object will be
|
Chris@0
|
295 * validated instead.
|
Chris@0
|
296 *
|
Chris@0
|
297 * @param object $object The object to cascade
|
Chris@0
|
298 * @param string $propertyPath The current property path
|
Chris@4
|
299 * @param (string|GroupSequence)[] $groups The validated groups
|
Chris@0
|
300 * @param int $traversalStrategy The strategy for traversing the
|
Chris@0
|
301 * cascaded object
|
Chris@0
|
302 * @param ExecutionContextInterface $context The current execution context
|
Chris@0
|
303 *
|
Chris@0
|
304 * @throws NoSuchMetadataException If the object has no associated metadata
|
Chris@0
|
305 * and does not implement {@link \Traversable}
|
Chris@0
|
306 * or if traversal is disabled via the
|
Chris@0
|
307 * $traversalStrategy argument
|
Chris@0
|
308 * @throws UnsupportedMetadataException If the metadata returned by the
|
Chris@0
|
309 * metadata factory does not implement
|
Chris@0
|
310 * {@link ClassMetadataInterface}
|
Chris@0
|
311 */
|
Chris@0
|
312 private function validateObject($object, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
|
Chris@0
|
313 {
|
Chris@0
|
314 try {
|
Chris@0
|
315 $classMetadata = $this->metadataFactory->getMetadataFor($object);
|
Chris@0
|
316
|
Chris@0
|
317 if (!$classMetadata instanceof ClassMetadataInterface) {
|
Chris@4
|
318 throw new UnsupportedMetadataException(sprintf('The metadata factory should return instances of "Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 $this->validateClassNode(
|
Chris@0
|
322 $object,
|
Chris@0
|
323 spl_object_hash($object),
|
Chris@0
|
324 $classMetadata,
|
Chris@0
|
325 $propertyPath,
|
Chris@0
|
326 $groups,
|
Chris@0
|
327 null,
|
Chris@0
|
328 $traversalStrategy,
|
Chris@0
|
329 $context
|
Chris@0
|
330 );
|
Chris@0
|
331 } catch (NoSuchMetadataException $e) {
|
Chris@0
|
332 // Rethrow if not Traversable
|
Chris@0
|
333 if (!$object instanceof \Traversable) {
|
Chris@0
|
334 throw $e;
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 // Rethrow unless IMPLICIT or TRAVERSE
|
Chris@0
|
338 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
|
Chris@0
|
339 throw $e;
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 $this->validateEachObjectIn(
|
Chris@0
|
343 $object,
|
Chris@0
|
344 $propertyPath,
|
Chris@0
|
345 $groups,
|
Chris@0
|
346 $context
|
Chris@0
|
347 );
|
Chris@0
|
348 }
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * Validates each object in a collection against the constraints defined
|
Chris@0
|
353 * for their classes.
|
Chris@0
|
354 *
|
Chris@0
|
355 * If the parameter $recursive is set to true, nested {@link \Traversable}
|
Chris@0
|
356 * objects are iterated as well. Nested arrays are always iterated,
|
Chris@0
|
357 * regardless of the value of $recursive.
|
Chris@0
|
358 *
|
Chris@0
|
359 * @param iterable $collection The collection
|
Chris@0
|
360 * @param string $propertyPath The current property path
|
Chris@4
|
361 * @param (string|GroupSequence)[] $groups The validated groups
|
Chris@0
|
362 * @param ExecutionContextInterface $context The current execution context
|
Chris@0
|
363 *
|
Chris@0
|
364 * @see ClassNode
|
Chris@0
|
365 * @see CollectionNode
|
Chris@0
|
366 */
|
Chris@0
|
367 private function validateEachObjectIn($collection, $propertyPath, array $groups, ExecutionContextInterface $context)
|
Chris@0
|
368 {
|
Chris@0
|
369 foreach ($collection as $key => $value) {
|
Chris@4
|
370 if (\is_array($value)) {
|
Chris@0
|
371 // Arrays are always cascaded, independent of the specified
|
Chris@0
|
372 // traversal strategy
|
Chris@0
|
373 $this->validateEachObjectIn(
|
Chris@0
|
374 $value,
|
Chris@0
|
375 $propertyPath.'['.$key.']',
|
Chris@0
|
376 $groups,
|
Chris@0
|
377 $context
|
Chris@0
|
378 );
|
Chris@0
|
379
|
Chris@0
|
380 continue;
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 // Scalar and null values in the collection are ignored
|
Chris@4
|
384 if (\is_object($value)) {
|
Chris@0
|
385 $this->validateObject(
|
Chris@0
|
386 $value,
|
Chris@0
|
387 $propertyPath.'['.$key.']',
|
Chris@0
|
388 $groups,
|
Chris@0
|
389 TraversalStrategy::IMPLICIT,
|
Chris@0
|
390 $context
|
Chris@0
|
391 );
|
Chris@0
|
392 }
|
Chris@0
|
393 }
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 /**
|
Chris@0
|
397 * Validates a class node.
|
Chris@0
|
398 *
|
Chris@0
|
399 * A class node is a combination of an object with a {@link ClassMetadataInterface}
|
Chris@0
|
400 * instance. Each class node (conceptionally) has zero or more succeeding
|
Chris@0
|
401 * property nodes:
|
Chris@0
|
402 *
|
Chris@0
|
403 * (Article:class node)
|
Chris@0
|
404 * \
|
Chris@0
|
405 * ($title:property node)
|
Chris@0
|
406 *
|
Chris@0
|
407 * This method validates the passed objects against all constraints defined
|
Chris@0
|
408 * at class level. It furthermore triggers the validation of each of the
|
Chris@0
|
409 * class' properties against the constraints for that property.
|
Chris@0
|
410 *
|
Chris@0
|
411 * If the selected traversal strategy allows traversal, the object is
|
Chris@0
|
412 * iterated and each nested object is validated against its own constraints.
|
Chris@0
|
413 * The object is not traversed if traversal is disabled in the class
|
Chris@0
|
414 * metadata.
|
Chris@0
|
415 *
|
Chris@0
|
416 * If the passed groups contain the group "Default", the validator will
|
Chris@0
|
417 * check whether the "Default" group has been replaced by a group sequence
|
Chris@0
|
418 * in the class metadata. If this is the case, the group sequence is
|
Chris@0
|
419 * validated instead.
|
Chris@0
|
420 *
|
Chris@0
|
421 * @param object $object The validated object
|
Chris@0
|
422 * @param string $cacheKey The key for caching
|
Chris@0
|
423 * the validated object
|
Chris@0
|
424 * @param ClassMetadataInterface $metadata The class metadata of
|
Chris@0
|
425 * the object
|
Chris@0
|
426 * @param string $propertyPath The property path leading
|
Chris@0
|
427 * to the object
|
Chris@4
|
428 * @param (string|GroupSequence)[] $groups The groups in which the
|
Chris@0
|
429 * object should be validated
|
Chris@0
|
430 * @param string[]|null $cascadedGroups The groups in which
|
Chris@0
|
431 * cascaded objects should
|
Chris@0
|
432 * be validated
|
Chris@0
|
433 * @param int $traversalStrategy The strategy used for
|
Chris@0
|
434 * traversing the object
|
Chris@0
|
435 * @param ExecutionContextInterface $context The current execution context
|
Chris@0
|
436 *
|
Chris@0
|
437 * @throws UnsupportedMetadataException If a property metadata does not
|
Chris@0
|
438 * implement {@link PropertyMetadataInterface}
|
Chris@0
|
439 * @throws ConstraintDefinitionException If traversal was enabled but the
|
Chris@0
|
440 * object does not implement
|
Chris@0
|
441 * {@link \Traversable}
|
Chris@0
|
442 *
|
Chris@0
|
443 * @see TraversalStrategy
|
Chris@0
|
444 */
|
Chris@0
|
445 private function validateClassNode($object, $cacheKey, ClassMetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
|
Chris@0
|
446 {
|
Chris@0
|
447 $context->setNode($object, $object, $metadata, $propertyPath);
|
Chris@0
|
448
|
Chris@0
|
449 if (!$context->isObjectInitialized($cacheKey)) {
|
Chris@0
|
450 foreach ($this->objectInitializers as $initializer) {
|
Chris@0
|
451 $initializer->initialize($object);
|
Chris@0
|
452 }
|
Chris@0
|
453
|
Chris@0
|
454 $context->markObjectAsInitialized($cacheKey);
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 foreach ($groups as $key => $group) {
|
Chris@0
|
458 // If the "Default" group is replaced by a group sequence, remember
|
Chris@0
|
459 // to cascade the "Default" group when traversing the group
|
Chris@0
|
460 // sequence
|
Chris@0
|
461 $defaultOverridden = false;
|
Chris@0
|
462
|
Chris@0
|
463 // Use the object hash for group sequences
|
Chris@4
|
464 $groupHash = \is_object($group) ? spl_object_hash($group) : $group;
|
Chris@0
|
465
|
Chris@0
|
466 if ($context->isGroupValidated($cacheKey, $groupHash)) {
|
Chris@0
|
467 // Skip this group when validating the properties and when
|
Chris@0
|
468 // traversing the object
|
Chris@0
|
469 unset($groups[$key]);
|
Chris@0
|
470
|
Chris@0
|
471 continue;
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 $context->markGroupAsValidated($cacheKey, $groupHash);
|
Chris@0
|
475
|
Chris@0
|
476 // Replace the "Default" group by the group sequence defined
|
Chris@0
|
477 // for the class, if applicable.
|
Chris@0
|
478 // This is done after checking the cache, so that
|
Chris@0
|
479 // spl_object_hash() isn't called for this sequence and
|
Chris@0
|
480 // "Default" is used instead in the cache. This is useful
|
Chris@0
|
481 // if the getters below return different group sequences in
|
Chris@0
|
482 // every call.
|
Chris@0
|
483 if (Constraint::DEFAULT_GROUP === $group) {
|
Chris@0
|
484 if ($metadata->hasGroupSequence()) {
|
Chris@0
|
485 // The group sequence is statically defined for the class
|
Chris@0
|
486 $group = $metadata->getGroupSequence();
|
Chris@0
|
487 $defaultOverridden = true;
|
Chris@0
|
488 } elseif ($metadata->isGroupSequenceProvider()) {
|
Chris@0
|
489 // The group sequence is dynamically obtained from the validated
|
Chris@0
|
490 // object
|
Chris@0
|
491 /* @var \Symfony\Component\Validator\GroupSequenceProviderInterface $object */
|
Chris@0
|
492 $group = $object->getGroupSequence();
|
Chris@0
|
493 $defaultOverridden = true;
|
Chris@0
|
494
|
Chris@0
|
495 if (!$group instanceof GroupSequence) {
|
Chris@0
|
496 $group = new GroupSequence($group);
|
Chris@0
|
497 }
|
Chris@0
|
498 }
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 // If the groups (=[<G1,G2>,G3,G4]) contain a group sequence
|
Chris@0
|
502 // (=<G1,G2>), then call validateClassNode() with each entry of the
|
Chris@0
|
503 // group sequence and abort if necessary (G1, G2)
|
Chris@0
|
504 if ($group instanceof GroupSequence) {
|
Chris@0
|
505 $this->stepThroughGroupSequence(
|
Chris@0
|
506 $object,
|
Chris@0
|
507 $object,
|
Chris@0
|
508 $cacheKey,
|
Chris@0
|
509 $metadata,
|
Chris@0
|
510 $propertyPath,
|
Chris@0
|
511 $traversalStrategy,
|
Chris@0
|
512 $group,
|
Chris@0
|
513 $defaultOverridden ? Constraint::DEFAULT_GROUP : null,
|
Chris@0
|
514 $context
|
Chris@0
|
515 );
|
Chris@0
|
516
|
Chris@0
|
517 // Skip the group sequence when validating properties, because
|
Chris@0
|
518 // stepThroughGroupSequence() already validates the properties
|
Chris@0
|
519 unset($groups[$key]);
|
Chris@0
|
520
|
Chris@0
|
521 continue;
|
Chris@0
|
522 }
|
Chris@0
|
523
|
Chris@0
|
524 $this->validateInGroup($object, $cacheKey, $metadata, $group, $context);
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@0
|
527 // If no more groups should be validated for the property nodes,
|
Chris@0
|
528 // we can safely quit
|
Chris@4
|
529 if (0 === \count($groups)) {
|
Chris@0
|
530 return;
|
Chris@0
|
531 }
|
Chris@0
|
532
|
Chris@0
|
533 // Validate all properties against their constraints
|
Chris@0
|
534 foreach ($metadata->getConstrainedProperties() as $propertyName) {
|
Chris@0
|
535 // If constraints are defined both on the getter of a property as
|
Chris@0
|
536 // well as on the property itself, then getPropertyMetadata()
|
Chris@0
|
537 // returns two metadata objects, not just one
|
Chris@0
|
538 foreach ($metadata->getPropertyMetadata($propertyName) as $propertyMetadata) {
|
Chris@0
|
539 if (!$propertyMetadata instanceof PropertyMetadataInterface) {
|
Chris@4
|
540 throw new UnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".', \is_object($propertyMetadata) ? \get_class($propertyMetadata) : \gettype($propertyMetadata)));
|
Chris@0
|
541 }
|
Chris@0
|
542
|
Chris@0
|
543 $propertyValue = $propertyMetadata->getPropertyValue($object);
|
Chris@0
|
544
|
Chris@0
|
545 $this->validateGenericNode(
|
Chris@0
|
546 $propertyValue,
|
Chris@0
|
547 $object,
|
Chris@4
|
548 $cacheKey.':'.\get_class($object).':'.$propertyName,
|
Chris@0
|
549 $propertyMetadata,
|
Chris@0
|
550 PropertyPath::append($propertyPath, $propertyName),
|
Chris@0
|
551 $groups,
|
Chris@0
|
552 $cascadedGroups,
|
Chris@0
|
553 TraversalStrategy::IMPLICIT,
|
Chris@0
|
554 $context
|
Chris@0
|
555 );
|
Chris@0
|
556 }
|
Chris@0
|
557 }
|
Chris@0
|
558
|
Chris@0
|
559 // If no specific traversal strategy was requested when this method
|
Chris@0
|
560 // was called, use the traversal strategy of the class' metadata
|
Chris@0
|
561 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
|
Chris@0
|
562 $traversalStrategy = $metadata->getTraversalStrategy();
|
Chris@0
|
563 }
|
Chris@0
|
564
|
Chris@0
|
565 // Traverse only if IMPLICIT or TRAVERSE
|
Chris@0
|
566 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
|
Chris@0
|
567 return;
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 // If IMPLICIT, stop unless we deal with a Traversable
|
Chris@0
|
571 if ($traversalStrategy & TraversalStrategy::IMPLICIT && !$object instanceof \Traversable) {
|
Chris@0
|
572 return;
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 // If TRAVERSE, fail if we have no Traversable
|
Chris@0
|
576 if (!$object instanceof \Traversable) {
|
Chris@4
|
577 throw new ConstraintDefinitionException(sprintf('Traversal was enabled for "%s", but this class does not implement "\Traversable".', \get_class($object)));
|
Chris@0
|
578 }
|
Chris@0
|
579
|
Chris@0
|
580 $this->validateEachObjectIn(
|
Chris@0
|
581 $object,
|
Chris@0
|
582 $propertyPath,
|
Chris@0
|
583 $groups,
|
Chris@0
|
584 $context
|
Chris@0
|
585 );
|
Chris@0
|
586 }
|
Chris@0
|
587
|
Chris@0
|
588 /**
|
Chris@0
|
589 * Validates a node that is not a class node.
|
Chris@0
|
590 *
|
Chris@0
|
591 * Currently, two such node types exist:
|
Chris@0
|
592 *
|
Chris@0
|
593 * - property nodes, which consist of the value of an object's
|
Chris@0
|
594 * property together with a {@link PropertyMetadataInterface} instance
|
Chris@0
|
595 * - generic nodes, which consist of a value and some arbitrary
|
Chris@0
|
596 * constraints defined in a {@link MetadataInterface} container
|
Chris@0
|
597 *
|
Chris@0
|
598 * In both cases, the value is validated against all constraints defined
|
Chris@0
|
599 * in the passed metadata object. Then, if the value is an instance of
|
Chris@0
|
600 * {@link \Traversable} and the selected traversal strategy permits it,
|
Chris@0
|
601 * the value is traversed and each nested object validated against its own
|
Chris@0
|
602 * constraints. Arrays are always traversed.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @param mixed $value The validated value
|
Chris@0
|
605 * @param object|null $object The current object
|
Chris@0
|
606 * @param string $cacheKey The key for caching
|
Chris@0
|
607 * the validated value
|
Chris@0
|
608 * @param MetadataInterface $metadata The metadata of the
|
Chris@0
|
609 * value
|
Chris@0
|
610 * @param string $propertyPath The property path leading
|
Chris@0
|
611 * to the value
|
Chris@4
|
612 * @param (string|GroupSequence)[] $groups The groups in which the
|
Chris@0
|
613 * value should be validated
|
Chris@0
|
614 * @param string[]|null $cascadedGroups The groups in which
|
Chris@0
|
615 * cascaded objects should
|
Chris@0
|
616 * be validated
|
Chris@0
|
617 * @param int $traversalStrategy The strategy used for
|
Chris@0
|
618 * traversing the value
|
Chris@0
|
619 * @param ExecutionContextInterface $context The current execution context
|
Chris@0
|
620 *
|
Chris@0
|
621 * @see TraversalStrategy
|
Chris@0
|
622 */
|
Chris@0
|
623 private function validateGenericNode($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
|
Chris@0
|
624 {
|
Chris@0
|
625 $context->setNode($value, $object, $metadata, $propertyPath);
|
Chris@0
|
626
|
Chris@0
|
627 foreach ($groups as $key => $group) {
|
Chris@0
|
628 if ($group instanceof GroupSequence) {
|
Chris@0
|
629 $this->stepThroughGroupSequence(
|
Chris@0
|
630 $value,
|
Chris@0
|
631 $object,
|
Chris@0
|
632 $cacheKey,
|
Chris@0
|
633 $metadata,
|
Chris@0
|
634 $propertyPath,
|
Chris@0
|
635 $traversalStrategy,
|
Chris@0
|
636 $group,
|
Chris@0
|
637 null,
|
Chris@0
|
638 $context
|
Chris@0
|
639 );
|
Chris@0
|
640
|
Chris@0
|
641 // Skip the group sequence when cascading, as the cascading
|
Chris@0
|
642 // logic is already done in stepThroughGroupSequence()
|
Chris@0
|
643 unset($groups[$key]);
|
Chris@0
|
644
|
Chris@0
|
645 continue;
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 $this->validateInGroup($value, $cacheKey, $metadata, $group, $context);
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@4
|
651 if (0 === \count($groups)) {
|
Chris@0
|
652 return;
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 if (null === $value) {
|
Chris@0
|
656 return;
|
Chris@0
|
657 }
|
Chris@0
|
658
|
Chris@0
|
659 $cascadingStrategy = $metadata->getCascadingStrategy();
|
Chris@0
|
660
|
Chris@0
|
661 // Quit unless we have an array or a cascaded object
|
Chris@4
|
662 if (!\is_array($value) && !($cascadingStrategy & CascadingStrategy::CASCADE)) {
|
Chris@0
|
663 return;
|
Chris@0
|
664 }
|
Chris@0
|
665
|
Chris@0
|
666 // If no specific traversal strategy was requested when this method
|
Chris@0
|
667 // was called, use the traversal strategy of the node's metadata
|
Chris@0
|
668 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
|
Chris@0
|
669 $traversalStrategy = $metadata->getTraversalStrategy();
|
Chris@0
|
670 }
|
Chris@0
|
671
|
Chris@0
|
672 // The $cascadedGroups property is set, if the "Default" group is
|
Chris@0
|
673 // overridden by a group sequence
|
Chris@0
|
674 // See validateClassNode()
|
Chris@4
|
675 $cascadedGroups = null !== $cascadedGroups && \count($cascadedGroups) > 0 ? $cascadedGroups : $groups;
|
Chris@0
|
676
|
Chris@4
|
677 if (\is_array($value)) {
|
Chris@0
|
678 // Arrays are always traversed, independent of the specified
|
Chris@0
|
679 // traversal strategy
|
Chris@0
|
680 $this->validateEachObjectIn(
|
Chris@0
|
681 $value,
|
Chris@0
|
682 $propertyPath,
|
Chris@0
|
683 $cascadedGroups,
|
Chris@0
|
684 $context
|
Chris@0
|
685 );
|
Chris@0
|
686
|
Chris@0
|
687 return;
|
Chris@0
|
688 }
|
Chris@0
|
689
|
Chris@0
|
690 // If the value is a scalar, pass it anyway, because we want
|
Chris@0
|
691 // a NoSuchMetadataException to be thrown in that case
|
Chris@0
|
692 $this->validateObject(
|
Chris@0
|
693 $value,
|
Chris@0
|
694 $propertyPath,
|
Chris@0
|
695 $cascadedGroups,
|
Chris@0
|
696 $traversalStrategy,
|
Chris@0
|
697 $context
|
Chris@0
|
698 );
|
Chris@0
|
699
|
Chris@0
|
700 // Currently, the traversal strategy can only be TRAVERSE for a
|
Chris@0
|
701 // generic node if the cascading strategy is CASCADE. Thus, traversable
|
Chris@0
|
702 // objects will always be handled within validateObject() and there's
|
Chris@0
|
703 // nothing more to do here.
|
Chris@0
|
704
|
Chris@0
|
705 // see GenericMetadata::addConstraint()
|
Chris@0
|
706 }
|
Chris@0
|
707
|
Chris@0
|
708 /**
|
Chris@0
|
709 * Sequentially validates a node's value in each group of a group sequence.
|
Chris@0
|
710 *
|
Chris@0
|
711 * If any of the constraints generates a violation, subsequent groups in the
|
Chris@0
|
712 * group sequence are skipped.
|
Chris@0
|
713 *
|
Chris@0
|
714 * @param mixed $value The validated value
|
Chris@0
|
715 * @param object|null $object The current object
|
Chris@0
|
716 * @param string $cacheKey The key for caching
|
Chris@0
|
717 * the validated value
|
Chris@0
|
718 * @param MetadataInterface $metadata The metadata of the
|
Chris@0
|
719 * value
|
Chris@0
|
720 * @param string $propertyPath The property path leading
|
Chris@0
|
721 * to the value
|
Chris@0
|
722 * @param int $traversalStrategy The strategy used for
|
Chris@0
|
723 * traversing the value
|
Chris@0
|
724 * @param GroupSequence $groupSequence The group sequence
|
Chris@0
|
725 * @param string|null $cascadedGroup The group that should
|
Chris@0
|
726 * be passed to cascaded
|
Chris@0
|
727 * objects instead of
|
Chris@0
|
728 * the group sequence
|
Chris@0
|
729 * @param ExecutionContextInterface $context The execution context
|
Chris@0
|
730 */
|
Chris@0
|
731 private function stepThroughGroupSequence($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, $traversalStrategy, GroupSequence $groupSequence, $cascadedGroup, ExecutionContextInterface $context)
|
Chris@0
|
732 {
|
Chris@4
|
733 $violationCount = \count($context->getViolations());
|
Chris@4
|
734 $cascadedGroups = $cascadedGroup ? [$cascadedGroup] : null;
|
Chris@0
|
735
|
Chris@0
|
736 foreach ($groupSequence->groups as $groupInSequence) {
|
Chris@0
|
737 $groups = (array) $groupInSequence;
|
Chris@0
|
738
|
Chris@0
|
739 if ($metadata instanceof ClassMetadataInterface) {
|
Chris@0
|
740 $this->validateClassNode(
|
Chris@0
|
741 $value,
|
Chris@0
|
742 $cacheKey,
|
Chris@0
|
743 $metadata,
|
Chris@0
|
744 $propertyPath,
|
Chris@0
|
745 $groups,
|
Chris@0
|
746 $cascadedGroups,
|
Chris@0
|
747 $traversalStrategy,
|
Chris@0
|
748 $context
|
Chris@0
|
749 );
|
Chris@0
|
750 } else {
|
Chris@0
|
751 $this->validateGenericNode(
|
Chris@0
|
752 $value,
|
Chris@0
|
753 $object,
|
Chris@0
|
754 $cacheKey,
|
Chris@0
|
755 $metadata,
|
Chris@0
|
756 $propertyPath,
|
Chris@0
|
757 $groups,
|
Chris@0
|
758 $cascadedGroups,
|
Chris@0
|
759 $traversalStrategy,
|
Chris@0
|
760 $context
|
Chris@0
|
761 );
|
Chris@0
|
762 }
|
Chris@0
|
763
|
Chris@0
|
764 // Abort sequence validation if a violation was generated
|
Chris@4
|
765 if (\count($context->getViolations()) > $violationCount) {
|
Chris@0
|
766 break;
|
Chris@0
|
767 }
|
Chris@0
|
768 }
|
Chris@0
|
769 }
|
Chris@0
|
770
|
Chris@0
|
771 /**
|
Chris@0
|
772 * Validates a node's value against all constraints in the given group.
|
Chris@0
|
773 *
|
Chris@0
|
774 * @param mixed $value The validated value
|
Chris@0
|
775 * @param string $cacheKey The key for caching the
|
Chris@0
|
776 * validated value
|
Chris@0
|
777 * @param MetadataInterface $metadata The metadata of the value
|
Chris@0
|
778 * @param string $group The group to validate
|
Chris@0
|
779 * @param ExecutionContextInterface $context The execution context
|
Chris@0
|
780 */
|
Chris@0
|
781 private function validateInGroup($value, $cacheKey, MetadataInterface $metadata, $group, ExecutionContextInterface $context)
|
Chris@0
|
782 {
|
Chris@0
|
783 $context->setGroup($group);
|
Chris@0
|
784
|
Chris@0
|
785 foreach ($metadata->findConstraints($group) as $constraint) {
|
Chris@0
|
786 // Prevent duplicate validation of constraints, in the case
|
Chris@0
|
787 // that constraints belong to multiple validated groups
|
Chris@0
|
788 if (null !== $cacheKey) {
|
Chris@0
|
789 $constraintHash = spl_object_hash($constraint);
|
Chris@0
|
790
|
Chris@4
|
791 if ($constraint instanceof Composite) {
|
Chris@4
|
792 $constraintHash .= $group;
|
Chris@4
|
793 }
|
Chris@4
|
794
|
Chris@0
|
795 if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
|
Chris@0
|
796 continue;
|
Chris@0
|
797 }
|
Chris@0
|
798
|
Chris@0
|
799 $context->markConstraintAsValidated($cacheKey, $constraintHash);
|
Chris@0
|
800 }
|
Chris@0
|
801
|
Chris@0
|
802 $context->setConstraint($constraint);
|
Chris@0
|
803
|
Chris@0
|
804 $validator = $this->validatorFactory->getInstance($constraint);
|
Chris@0
|
805 $validator->initialize($context);
|
Chris@0
|
806 $validator->validate($value, $constraint);
|
Chris@0
|
807 }
|
Chris@0
|
808 }
|
Chris@0
|
809 }
|