4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Validator;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Constraints\GroupSequence;
16 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
17 use Symfony\Component\Validator\Context\ExecutionContext;
18 use Symfony\Component\Validator\Context\ExecutionContextInterface;
19 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
20 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
21 use Symfony\Component\Validator\Exception\RuntimeException;
22 use Symfony\Component\Validator\Exception\UnsupportedMetadataException;
23 use Symfony\Component\Validator\Exception\ValidatorException;
24 use Symfony\Component\Validator\Mapping\CascadingStrategy;
25 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
26 use Symfony\Component\Validator\Mapping\GenericMetadata;
27 use Symfony\Component\Validator\Mapping\MetadataInterface;
28 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
29 use Symfony\Component\Validator\Mapping\TraversalStrategy;
30 use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
31 use Symfony\Component\Validator\ObjectInitializerInterface;
32 use Symfony\Component\Validator\Util\PropertyPath;
35 * Recursive implementation of {@link ContextualValidatorInterface}.
37 * @author Bernhard Schussek <bschussek@gmail.com>
39 class RecursiveContextualValidator implements ContextualValidatorInterface
42 * @var ExecutionContextInterface
49 private $defaultPropertyPath;
54 private $defaultGroups;
57 * @var MetadataFactoryInterface
59 private $metadataFactory;
62 * @var ConstraintValidatorFactoryInterface
64 private $validatorFactory;
67 * @var ObjectInitializerInterface[]
69 private $objectInitializers;
72 * Creates a validator for the given context.
74 * @param ExecutionContextInterface $context The execution context
75 * @param MetadataFactoryInterface $metadataFactory The factory for
76 * fetching the metadata
77 * of validated objects
78 * @param ConstraintValidatorFactoryInterface $validatorFactory The factory for creating
79 * constraint validators
80 * @param ObjectInitializerInterface[] $objectInitializers The object initializers
82 public function __construct(ExecutionContextInterface $context, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = array())
84 $this->context = $context;
85 $this->defaultPropertyPath = $context->getPropertyPath();
86 $this->defaultGroups = array($context->getGroup() ?: Constraint::DEFAULT_GROUP);
87 $this->metadataFactory = $metadataFactory;
88 $this->validatorFactory = $validatorFactory;
89 $this->objectInitializers = $objectInitializers;
95 public function atPath($path)
97 $this->defaultPropertyPath = $this->context->getPropertyPath($path);
105 public function validate($value, $constraints = null, $groups = null)
107 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
109 $previousValue = $this->context->getValue();
110 $previousObject = $this->context->getObject();
111 $previousMetadata = $this->context->getMetadata();
112 $previousPath = $this->context->getPropertyPath();
113 $previousGroup = $this->context->getGroup();
114 $previousConstraint = null;
116 if ($this->context instanceof ExecutionContext || method_exists($this->context, 'getConstraint')) {
117 $previousConstraint = $this->context->getConstraint();
120 // If explicit constraints are passed, validate the value against
122 if (null !== $constraints) {
123 // You can pass a single constraint or an array of constraints
124 // Make sure to deal with an array in the rest of the code
125 if (!is_array($constraints)) {
126 $constraints = array($constraints);
129 $metadata = new GenericMetadata();
130 $metadata->addConstraints($constraints);
132 $this->validateGenericNode(
135 is_object($value) ? spl_object_hash($value) : null,
137 $this->defaultPropertyPath,
140 TraversalStrategy::IMPLICIT,
144 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
145 $this->context->setGroup($previousGroup);
147 if (null !== $previousConstraint) {
148 $this->context->setConstraint($previousConstraint);
154 // If an object is passed without explicit constraints, validate that
155 // object against the constraints defined for the object's class
156 if (is_object($value)) {
157 $this->validateObject(
159 $this->defaultPropertyPath,
161 TraversalStrategy::IMPLICIT,
165 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
166 $this->context->setGroup($previousGroup);
171 // If an array is passed without explicit constraints, validate each
172 // object in the array
173 if (is_array($value)) {
174 $this->validateEachObjectIn(
176 $this->defaultPropertyPath,
181 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
182 $this->context->setGroup($previousGroup);
187 throw new RuntimeException(sprintf(
188 'Cannot validate values of type "%s" automatically. Please '.
189 'provide a constraint.',
197 public function validateProperty($object, $propertyName, $groups = null)
199 $classMetadata = $this->metadataFactory->getMetadataFor($object);
201 if (!$classMetadata instanceof ClassMetadataInterface) {
202 throw new ValidatorException(sprintf(
203 'The metadata factory should return instances of '.
204 '"\Symfony\Component\Validator\Mapping\ClassMetadataInterface", '.
206 is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)
210 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
211 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
212 $cacheKey = spl_object_hash($object);
213 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
215 $previousValue = $this->context->getValue();
216 $previousObject = $this->context->getObject();
217 $previousMetadata = $this->context->getMetadata();
218 $previousPath = $this->context->getPropertyPath();
219 $previousGroup = $this->context->getGroup();
221 foreach ($propertyMetadatas as $propertyMetadata) {
222 $propertyValue = $propertyMetadata->getPropertyValue($object);
224 $this->validateGenericNode(
227 $cacheKey.':'.get_class($object).':'.$propertyName,
232 TraversalStrategy::IMPLICIT,
237 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
238 $this->context->setGroup($previousGroup);
246 public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
248 $classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass);
250 if (!$classMetadata instanceof ClassMetadataInterface) {
251 throw new ValidatorException(sprintf(
252 'The metadata factory should return instances of '.
253 '"\Symfony\Component\Validator\Mapping\ClassMetadataInterface", '.
255 is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)
259 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
260 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
262 if (is_object($objectOrClass)) {
263 $object = $objectOrClass;
264 $cacheKey = spl_object_hash($objectOrClass);
265 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
267 // $objectOrClass contains a class name
270 $propertyPath = $this->defaultPropertyPath;
273 $previousValue = $this->context->getValue();
274 $previousObject = $this->context->getObject();
275 $previousMetadata = $this->context->getMetadata();
276 $previousPath = $this->context->getPropertyPath();
277 $previousGroup = $this->context->getGroup();
279 foreach ($propertyMetadatas as $propertyMetadata) {
280 $this->validateGenericNode(
283 $cacheKey.':'.get_class($object).':'.$propertyName,
288 TraversalStrategy::IMPLICIT,
293 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
294 $this->context->setGroup($previousGroup);
302 public function getViolations()
304 return $this->context->getViolations();
308 * Normalizes the given group or list of groups to an array.
310 * @param mixed $groups The groups to normalize
312 * @return array A group array
314 protected function normalizeGroups($groups)
316 if (is_array($groups)) {
320 return array($groups);
324 * Validates an object against the constraints defined for its class.
326 * If no metadata is available for the class, but the class is an instance
327 * of {@link \Traversable} and the selected traversal strategy allows
328 * traversal, the object will be iterated and each nested object will be
331 * @param object $object The object to cascade
332 * @param string $propertyPath The current property path
333 * @param string[] $groups The validated groups
334 * @param int $traversalStrategy The strategy for traversing the
336 * @param ExecutionContextInterface $context The current execution context
338 * @throws NoSuchMetadataException If the object has no associated metadata
339 * and does not implement {@link \Traversable}
340 * or if traversal is disabled via the
341 * $traversalStrategy argument
342 * @throws UnsupportedMetadataException If the metadata returned by the
343 * metadata factory does not implement
344 * {@link ClassMetadataInterface}
346 private function validateObject($object, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
349 $classMetadata = $this->metadataFactory->getMetadataFor($object);
351 if (!$classMetadata instanceof ClassMetadataInterface) {
352 throw new UnsupportedMetadataException(sprintf(
353 'The metadata factory should return instances of '.
354 '"Symfony\Component\Validator\Mapping\ClassMetadataInterface", '.
356 is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)
360 $this->validateClassNode(
362 spl_object_hash($object),
370 } catch (NoSuchMetadataException $e) {
371 // Rethrow if not Traversable
372 if (!$object instanceof \Traversable) {
376 // Rethrow unless IMPLICIT or TRAVERSE
377 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
381 $this->validateEachObjectIn(
391 * Validates each object in a collection against the constraints defined
394 * If the parameter $recursive is set to true, nested {@link \Traversable}
395 * objects are iterated as well. Nested arrays are always iterated,
396 * regardless of the value of $recursive.
398 * @param array|\Traversable $collection The collection
399 * @param string $propertyPath The current property path
400 * @param string[] $groups The validated groups
401 * @param ExecutionContextInterface $context The current execution context
404 * @see CollectionNode
406 private function validateEachObjectIn($collection, $propertyPath, array $groups, ExecutionContextInterface $context)
408 foreach ($collection as $key => $value) {
409 if (is_array($value)) {
410 // Arrays are always cascaded, independent of the specified
411 // traversal strategy
412 $this->validateEachObjectIn(
414 $propertyPath.'['.$key.']',
422 // Scalar and null values in the collection are ignored
423 if (is_object($value)) {
424 $this->validateObject(
426 $propertyPath.'['.$key.']',
428 TraversalStrategy::IMPLICIT,
436 * Validates a class node.
438 * A class node is a combination of an object with a {@link ClassMetadataInterface}
439 * instance. Each class node (conceptionally) has zero or more succeeding
442 * (Article:class node)
444 * ($title:property node)
446 * This method validates the passed objects against all constraints defined
447 * at class level. It furthermore triggers the validation of each of the
448 * class' properties against the constraints for that property.
450 * If the selected traversal strategy allows traversal, the object is
451 * iterated and each nested object is validated against its own constraints.
452 * The object is not traversed if traversal is disabled in the class
455 * If the passed groups contain the group "Default", the validator will
456 * check whether the "Default" group has been replaced by a group sequence
457 * in the class metadata. If this is the case, the group sequence is
460 * @param object $object The validated object
461 * @param string $cacheKey The key for caching
462 * the validated object
463 * @param ClassMetadataInterface $metadata The class metadata of
465 * @param string $propertyPath The property path leading
467 * @param string[] $groups The groups in which the
468 * object should be validated
469 * @param string[]|null $cascadedGroups The groups in which
470 * cascaded objects should
472 * @param int $traversalStrategy The strategy used for
473 * traversing the object
474 * @param ExecutionContextInterface $context The current execution context
476 * @throws UnsupportedMetadataException If a property metadata does not
477 * implement {@link PropertyMetadataInterface}
478 * @throws ConstraintDefinitionException If traversal was enabled but the
479 * object does not implement
480 * {@link \Traversable}
482 * @see TraversalStrategy
484 private function validateClassNode($object, $cacheKey, ClassMetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
486 $context->setNode($object, $object, $metadata, $propertyPath);
488 if (!$context->isObjectInitialized($cacheKey)) {
489 foreach ($this->objectInitializers as $initializer) {
490 $initializer->initialize($object);
493 $context->markObjectAsInitialized($cacheKey);
496 foreach ($groups as $key => $group) {
497 // If the "Default" group is replaced by a group sequence, remember
498 // to cascade the "Default" group when traversing the group
500 $defaultOverridden = false;
502 // Use the object hash for group sequences
503 $groupHash = is_object($group) ? spl_object_hash($group) : $group;
505 if ($context->isGroupValidated($cacheKey, $groupHash)) {
506 // Skip this group when validating the properties and when
507 // traversing the object
508 unset($groups[$key]);
513 $context->markGroupAsValidated($cacheKey, $groupHash);
515 // Replace the "Default" group by the group sequence defined
516 // for the class, if applicable.
517 // This is done after checking the cache, so that
518 // spl_object_hash() isn't called for this sequence and
519 // "Default" is used instead in the cache. This is useful
520 // if the getters below return different group sequences in
522 if (Constraint::DEFAULT_GROUP === $group) {
523 if ($metadata->hasGroupSequence()) {
524 // The group sequence is statically defined for the class
525 $group = $metadata->getGroupSequence();
526 $defaultOverridden = true;
527 } elseif ($metadata->isGroupSequenceProvider()) {
528 // The group sequence is dynamically obtained from the validated
530 /* @var \Symfony\Component\Validator\GroupSequenceProviderInterface $object */
531 $group = $object->getGroupSequence();
532 $defaultOverridden = true;
534 if (!$group instanceof GroupSequence) {
535 $group = new GroupSequence($group);
540 // If the groups (=[<G1,G2>,G3,G4]) contain a group sequence
541 // (=<G1,G2>), then call validateClassNode() with each entry of the
542 // group sequence and abort if necessary (G1, G2)
543 if ($group instanceof GroupSequence) {
544 $this->stepThroughGroupSequence(
552 $defaultOverridden ? Constraint::DEFAULT_GROUP : null,
556 // Skip the group sequence when validating properties, because
557 // stepThroughGroupSequence() already validates the properties
558 unset($groups[$key]);
563 $this->validateInGroup($object, $cacheKey, $metadata, $group, $context);
566 // If no more groups should be validated for the property nodes,
567 // we can safely quit
568 if (0 === count($groups)) {
572 // Validate all properties against their constraints
573 foreach ($metadata->getConstrainedProperties() as $propertyName) {
574 // If constraints are defined both on the getter of a property as
575 // well as on the property itself, then getPropertyMetadata()
576 // returns two metadata objects, not just one
577 foreach ($metadata->getPropertyMetadata($propertyName) as $propertyMetadata) {
578 if (!$propertyMetadata instanceof PropertyMetadataInterface) {
579 throw new UnsupportedMetadataException(sprintf(
580 'The property metadata instances should implement '.
581 '"Symfony\Component\Validator\Mapping\PropertyMetadataInterface", '.
583 is_object($propertyMetadata) ? get_class($propertyMetadata) : gettype($propertyMetadata)
587 $propertyValue = $propertyMetadata->getPropertyValue($object);
589 $this->validateGenericNode(
592 $cacheKey.':'.get_class($object).':'.$propertyName,
594 PropertyPath::append($propertyPath, $propertyName),
597 TraversalStrategy::IMPLICIT,
603 // If no specific traversal strategy was requested when this method
604 // was called, use the traversal strategy of the class' metadata
605 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
606 $traversalStrategy = $metadata->getTraversalStrategy();
609 // Traverse only if IMPLICIT or TRAVERSE
610 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
614 // If IMPLICIT, stop unless we deal with a Traversable
615 if ($traversalStrategy & TraversalStrategy::IMPLICIT && !$object instanceof \Traversable) {
619 // If TRAVERSE, fail if we have no Traversable
620 if (!$object instanceof \Traversable) {
621 throw new ConstraintDefinitionException(sprintf(
622 'Traversal was enabled for "%s", but this class '.
623 'does not implement "\Traversable".',
628 $this->validateEachObjectIn(
637 * Validates a node that is not a class node.
639 * Currently, two such node types exist:
641 * - property nodes, which consist of the value of an object's
642 * property together with a {@link PropertyMetadataInterface} instance
643 * - generic nodes, which consist of a value and some arbitrary
644 * constraints defined in a {@link MetadataInterface} container
646 * In both cases, the value is validated against all constraints defined
647 * in the passed metadata object. Then, if the value is an instance of
648 * {@link \Traversable} and the selected traversal strategy permits it,
649 * the value is traversed and each nested object validated against its own
650 * constraints. Arrays are always traversed.
652 * @param mixed $value The validated value
653 * @param object|null $object The current object
654 * @param string $cacheKey The key for caching
655 * the validated value
656 * @param MetadataInterface $metadata The metadata of the
658 * @param string $propertyPath The property path leading
660 * @param string[] $groups The groups in which the
661 * value should be validated
662 * @param string[]|null $cascadedGroups The groups in which
663 * cascaded objects should
665 * @param int $traversalStrategy The strategy used for
666 * traversing the value
667 * @param ExecutionContextInterface $context The current execution context
669 * @see TraversalStrategy
671 private function validateGenericNode($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
673 $context->setNode($value, $object, $metadata, $propertyPath);
675 foreach ($groups as $key => $group) {
676 if ($group instanceof GroupSequence) {
677 $this->stepThroughGroupSequence(
689 // Skip the group sequence when cascading, as the cascading
690 // logic is already done in stepThroughGroupSequence()
691 unset($groups[$key]);
696 $this->validateInGroup($value, $cacheKey, $metadata, $group, $context);
699 if (0 === count($groups)) {
703 if (null === $value) {
707 $cascadingStrategy = $metadata->getCascadingStrategy();
709 // Quit unless we have an array or a cascaded object
710 if (!is_array($value) && !($cascadingStrategy & CascadingStrategy::CASCADE)) {
714 // If no specific traversal strategy was requested when this method
715 // was called, use the traversal strategy of the node's metadata
716 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
717 $traversalStrategy = $metadata->getTraversalStrategy();
720 // The $cascadedGroups property is set, if the "Default" group is
721 // overridden by a group sequence
722 // See validateClassNode()
723 $cascadedGroups = null !== $cascadedGroups && count($cascadedGroups) > 0 ? $cascadedGroups : $groups;
725 if (is_array($value)) {
726 // Arrays are always traversed, independent of the specified
727 // traversal strategy
728 $this->validateEachObjectIn(
738 // If the value is a scalar, pass it anyway, because we want
739 // a NoSuchMetadataException to be thrown in that case
740 $this->validateObject(
748 // Currently, the traversal strategy can only be TRAVERSE for a
749 // generic node if the cascading strategy is CASCADE. Thus, traversable
750 // objects will always be handled within validateObject() and there's
751 // nothing more to do here.
753 // see GenericMetadata::addConstraint()
757 * Sequentially validates a node's value in each group of a group sequence.
759 * If any of the constraints generates a violation, subsequent groups in the
760 * group sequence are skipped.
762 * @param mixed $value The validated value
763 * @param object|null $object The current object
764 * @param string $cacheKey The key for caching
765 * the validated value
766 * @param MetadataInterface $metadata The metadata of the
768 * @param string $propertyPath The property path leading
770 * @param int $traversalStrategy The strategy used for
771 * traversing the value
772 * @param GroupSequence $groupSequence The group sequence
773 * @param string|null $cascadedGroup The group that should
774 * be passed to cascaded
777 * @param ExecutionContextInterface $context The execution context
779 private function stepThroughGroupSequence($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, $traversalStrategy, GroupSequence $groupSequence, $cascadedGroup, ExecutionContextInterface $context)
781 $violationCount = count($context->getViolations());
782 $cascadedGroups = $cascadedGroup ? array($cascadedGroup) : null;
784 foreach ($groupSequence->groups as $groupInSequence) {
785 $groups = (array) $groupInSequence;
787 if ($metadata instanceof ClassMetadataInterface) {
788 $this->validateClassNode(
799 $this->validateGenericNode(
812 // Abort sequence validation if a violation was generated
813 if (count($context->getViolations()) > $violationCount) {
820 * Validates a node's value against all constraints in the given group.
822 * @param mixed $value The validated value
823 * @param string $cacheKey The key for caching the
825 * @param MetadataInterface $metadata The metadata of the value
826 * @param string $group The group to validate
827 * @param ExecutionContextInterface $context The execution context
829 private function validateInGroup($value, $cacheKey, MetadataInterface $metadata, $group, ExecutionContextInterface $context)
831 $context->setGroup($group);
833 foreach ($metadata->findConstraints($group) as $constraint) {
834 // Prevent duplicate validation of constraints, in the case
835 // that constraints belong to multiple validated groups
836 if (null !== $cacheKey) {
837 $constraintHash = spl_object_hash($constraint);
839 if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
843 $context->markConstraintAsValidated($cacheKey, $constraintHash);
846 $context->setConstraint($constraint);
848 $validator = $this->validatorFactory->getInstance($constraint);
849 $validator->initialize($context);
850 $validator->validate($value, $constraint);