vendor/api-platform/core/src/Symfony/Validator/Validator.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Validator;
  12. use ApiPlatform\Symfony\Validator\Exception\ValidationException;
  13. use ApiPlatform\Validator\ValidatorInterface;
  14. use Psr\Container\ContainerInterface;
  15. use Symfony\Component\Validator\Constraints\GroupSequence;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;
  17. /**
  18.  * Validates an item using the Symfony validator component.
  19.  *
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  *
  22.  * @final
  23.  */
  24. class Validator implements ValidatorInterface
  25. {
  26.     public function __construct(private readonly SymfonyValidatorInterface $validator, private readonly ?ContainerInterface $container null)
  27.     {
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function validate(object $data, array $context = []): void
  33.     {
  34.         if (null !== $validationGroups $context['groups'] ?? null) {
  35.             if (
  36.                 $this->container
  37.                 && \is_string($validationGroups)
  38.                 && $this->container->has($validationGroups)
  39.                 && ($service $this->container->get($validationGroups))
  40.                 && \is_callable($service)
  41.             ) {
  42.                 $validationGroups $service($data);
  43.             } elseif (\is_callable($validationGroups)) {
  44.                 $validationGroups $validationGroups($data);
  45.             }
  46.             if (!$validationGroups instanceof GroupSequence) {
  47.                 $validationGroups = (array) $validationGroups;
  48.             }
  49.         }
  50.         $violations $this->validator->validate($datanull$validationGroups);
  51.         if (!== \count($violations)) {
  52.             throw new ValidationException($violations);
  53.         }
  54.     }
  55. }