vendor/liip/functional-test-bundle/src/Validator/DataCollectingValidator.php line 89

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Liip/FunctionalTestBundle
  5.  *
  6.  * (c) Lukas Kahwe Smith <smith@pooteeweet.org>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Liip\FunctionalTestBundle\Validator;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Validator\ConstraintViolationList;
  16. use Symfony\Component\Validator\ConstraintViolationListInterface;
  17. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  18. use Symfony\Component\Validator\Mapping\MetadataInterface;
  19. use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
  20. use Symfony\Component\Validator\Validator\ValidatorInterface;
  21. class DataCollectingValidator implements ValidatorInterfaceEventSubscriberInterface
  22. {
  23.     /**
  24.      * @var ValidatorInterface
  25.      */
  26.     protected $wrappedValidator;
  27.     /**
  28.      * @var ConstraintViolationListInterface
  29.      */
  30.     protected $lastErrors;
  31.     public function __construct(ValidatorInterface $wrappedValidator)
  32.     {
  33.         $this->wrappedValidator $wrappedValidator;
  34.         $this->clearLastErrors();
  35.     }
  36.     public function clearLastErrors(): void
  37.     {
  38.         $this->lastErrors = new ConstraintViolationList();
  39.     }
  40.     public function getLastErrors(): ConstraintViolationListInterface
  41.     {
  42.         return $this->lastErrors;
  43.     }
  44.     public function getMetadataFor($value): MetadataInterface
  45.     {
  46.         return $this->wrappedValidator->getMetadataFor($value);
  47.     }
  48.     public function hasMetadataFor($value): bool
  49.     {
  50.         return $this->wrappedValidator->hasMetadataFor($value);
  51.     }
  52.     public function validate($value$constraints null$groups null): ConstraintViolationListInterface
  53.     {
  54.         return $this->lastErrors $this->wrappedValidator->validate($value$constraints$groups);
  55.     }
  56.     public function validateProperty($object$propertyName$groups null): ConstraintViolationListInterface
  57.     {
  58.         return $this->wrappedValidator->validateProperty($object$propertyName$groups);
  59.     }
  60.     public function validatePropertyValue($objectOrClass$propertyName$value$groups null): ConstraintViolationListInterface
  61.     {
  62.         return $this->wrappedValidator->validatePropertyValue($objectOrClass$propertyName$value$groups);
  63.     }
  64.     public function startContext(): ContextualValidatorInterface
  65.     {
  66.         return $this->wrappedValidator->startContext();
  67.     }
  68.     public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
  69.     {
  70.         return $this->wrappedValidator->inContext($context);
  71.     }
  72.     public function onKernelRequest(GetResponseEvent $event): void
  73.     {
  74.         if ($event->isMasterRequest()) {
  75.             $this->clearLastErrors();
  76.         }
  77.     }
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             KernelEvents::REQUEST => ['onKernelRequest'99999],
  82.         ];
  83.     }
  84. }