vendor/symfony/validator/Validator/RecursiveValidator.php line 82

  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\GroupSequence;
  13. use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
  14. use Symfony\Component\Validator\ConstraintViolationListInterface;
  15. use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
  16. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  17. use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
  18. use Symfony\Component\Validator\Mapping\MetadataInterface;
  19. use Symfony\Component\Validator\ObjectInitializerInterface;
  20. /**
  21.  * Recursive implementation of {@link ValidatorInterface}.
  22.  *
  23.  * @author Bernhard Schussek <bschussek@gmail.com>
  24.  */
  25. class RecursiveValidator implements ValidatorInterface
  26. {
  27.     protected $contextFactory;
  28.     protected $metadataFactory;
  29.     protected $validatorFactory;
  30.     protected $objectInitializers;
  31.     /**
  32.      * Creates a new validator.
  33.      *
  34.      * @param ObjectInitializerInterface[] $objectInitializers The object initializers
  35.      */
  36.     public function __construct(ExecutionContextFactoryInterface $contextFactoryMetadataFactoryInterface $metadataFactoryConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = [])
  37.     {
  38.         $this->contextFactory $contextFactory;
  39.         $this->metadataFactory $metadataFactory;
  40.         $this->validatorFactory $validatorFactory;
  41.         $this->objectInitializers $objectInitializers;
  42.     }
  43.     public function startContext(mixed $root null): ContextualValidatorInterface
  44.     {
  45.         return new RecursiveContextualValidator(
  46.             $this->contextFactory->createContext($this$root),
  47.             $this->metadataFactory,
  48.             $this->validatorFactory,
  49.             $this->objectInitializers
  50.         );
  51.     }
  52.     public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
  53.     {
  54.         return new RecursiveContextualValidator(
  55.             $context,
  56.             $this->metadataFactory,
  57.             $this->validatorFactory,
  58.             $this->objectInitializers
  59.         );
  60.     }
  61.     public function getMetadataFor(mixed $object): MetadataInterface
  62.     {
  63.         return $this->metadataFactory->getMetadataFor($object);
  64.     }
  65.     public function hasMetadataFor(mixed $object): bool
  66.     {
  67.         return $this->metadataFactory->hasMetadataFor($object);
  68.     }
  69.     public function validate(mixed $valueConstraint|array $constraints nullstring|GroupSequence|array $groups null): ConstraintViolationListInterface
  70.     {
  71.         return $this->startContext($value)
  72.             ->validate($value$constraints$groups)
  73.             ->getViolations();
  74.     }
  75.     public function validateProperty(object $objectstring $propertyNamestring|GroupSequence|array $groups null): ConstraintViolationListInterface
  76.     {
  77.         return $this->startContext($object)
  78.             ->validateProperty($object$propertyName$groups)
  79.             ->getViolations();
  80.     }
  81.     public function validatePropertyValue(object|string $objectOrClassstring $propertyNamemixed $valuestring|GroupSequence|array $groups null): ConstraintViolationListInterface
  82.     {
  83.         // If a class name is passed, take $value as root
  84.         return $this->startContext(\is_object($objectOrClass) ? $objectOrClass $value)
  85.             ->validatePropertyValue($objectOrClass$propertyName$value$groups)
  86.             ->getViolations();
  87.     }
  88. }