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

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.     private $validator;
  27.     private $container;
  28.     public function __construct(SymfonyValidatorInterface $validatorContainerInterface $container null)
  29.     {
  30.         $this->validator $validator;
  31.         $this->container $container;
  32.     }
  33.     public function validate($data, array $context = [])
  34.     {
  35.         if (null !== $validationGroups $context['groups'] ?? null) {
  36.             if (
  37.                 $this->container
  38.                 && \is_string($validationGroups)
  39.                 && $this->container->has($validationGroups)
  40.                 && ($service $this->container->get($validationGroups))
  41.                 && \is_callable($service)
  42.             ) {
  43.                 if (!$service instanceof ValidationGroupsGeneratorInterface) {
  44.                     @trigger_error(sprintf('Using a public validation groups generator service not implementing "%s" is deprecated since 2.6 and will be removed in 3.0.'ValidationGroupsGeneratorInterface::class), \E_USER_DEPRECATED);
  45.                 }
  46.                 $validationGroups $service($data);
  47.             } elseif (\is_callable($validationGroups)) {
  48.                 $validationGroups $validationGroups($data);
  49.             }
  50.             if (!$validationGroups instanceof GroupSequence) {
  51.                 $validationGroups = (array) $validationGroups;
  52.             }
  53.         }
  54.         $violations $this->validator->validate($datanull$validationGroups);
  55.         if (!== \count($violations)) {
  56.             throw new ValidationException($violations);
  57.         }
  58.     }
  59. }
  60. class_alias(Validator::class, \ApiPlatform\Core\Bridge\Symfony\Validator\Validator::class);