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

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\Core\Bridge\Symfony\Validator;
  12. use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
  13. use ApiPlatform\Core\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. class Validator implements ValidatorInterface
  23. {
  24.     private $validator;
  25.     private $container;
  26.     public function __construct(SymfonyValidatorInterface $validatorContainerInterface $container null)
  27.     {
  28.         $this->validator $validator;
  29.         $this->container $container;
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function validate($data, array $context = [])
  35.     {
  36.         if (null !== $validationGroups $context['groups'] ?? null) {
  37.             if (
  38.                 $this->container &&
  39.                 \is_string($validationGroups) &&
  40.                 $this->container->has($validationGroups) &&
  41.                 ($service $this->container->get($validationGroups)) &&
  42.                 \is_callable($service)
  43.             ) {
  44.                 $validationGroups $service($data);
  45.             } elseif (\is_callable($validationGroups)) {
  46.                 $validationGroups $validationGroups($data);
  47.             }
  48.             if (!$validationGroups instanceof GroupSequence) {
  49.                 $validationGroups = (array) $validationGroups;
  50.             }
  51.         }
  52.         $violations $this->validator->validate($datanull$validationGroups);
  53.         if (!== \count($violations)) {
  54.             throw new ValidationException($violations);
  55.         }
  56.     }
  57. }