vendor/symfony/validator/Validator/RecursiveContextualValidator.php line 412

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