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

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