vendor/symfony/validator/Validator/TraceableValidator.php line 58

  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\ConstraintViolationListInterface;
  14. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  15. use Symfony\Component\Validator\Mapping\MetadataInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18.  * Collects some data about validator calls.
  19.  *
  20.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  21.  */
  22. class TraceableValidator implements ValidatorInterfaceResetInterface
  23. {
  24.     private ValidatorInterface $validator;
  25.     private array $collectedData = [];
  26.     public function __construct(ValidatorInterface $validator)
  27.     {
  28.         $this->validator $validator;
  29.     }
  30.     public function getCollectedData(): array
  31.     {
  32.         return $this->collectedData;
  33.     }
  34.     public function reset()
  35.     {
  36.         $this->collectedData = [];
  37.     }
  38.     public function getMetadataFor(mixed $value): MetadataInterface
  39.     {
  40.         return $this->validator->getMetadataFor($value);
  41.     }
  42.     public function hasMetadataFor(mixed $value): bool
  43.     {
  44.         return $this->validator->hasMetadataFor($value);
  45.     }
  46.     public function validate(mixed $valueConstraint|array $constraints nullstring|GroupSequence|array $groups null): ConstraintViolationListInterface
  47.     {
  48.         $violations $this->validator->validate($value$constraints$groups);
  49.         $trace debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS7);
  50.         $file $trace[0]['file'];
  51.         $line $trace[0]['line'];
  52.         for ($i 1$i 7; ++$i) {
  53.             if (isset($trace[$i]['class'], $trace[$i]['function'])
  54.                 && 'validate' === $trace[$i]['function']
  55.                 && is_a($trace[$i]['class'], ValidatorInterface::class, true)
  56.             ) {
  57.                 $file $trace[$i]['file'];
  58.                 $line $trace[$i]['line'];
  59.                 while (++$i 7) {
  60.                     if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && !str_starts_with($trace[$i]['function'], 'call_user_func')) {
  61.                         $file $trace[$i]['file'];
  62.                         $line $trace[$i]['line'];
  63.                         break;
  64.                     }
  65.                 }
  66.                 break;
  67.             }
  68.         }
  69.         $name str_replace('\\''/'$file);
  70.         $name substr($namestrrpos($name'/') + 1);
  71.         $this->collectedData[] = [
  72.             'caller' => compact('name''file''line'),
  73.             'context' => compact('value''constraints''groups'),
  74.             'violations' => iterator_to_array($violations),
  75.         ];
  76.         return $violations;
  77.     }
  78.     public function validateProperty(object $objectstring $propertyNamestring|GroupSequence|array $groups null): ConstraintViolationListInterface
  79.     {
  80.         return $this->validator->validateProperty($object$propertyName$groups);
  81.     }
  82.     public function validatePropertyValue(object|string $objectOrClassstring $propertyNamemixed $valuestring|GroupSequence|array $groups null): ConstraintViolationListInterface
  83.     {
  84.         return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
  85.     }
  86.     public function startContext(): ContextualValidatorInterface
  87.     {
  88.         return $this->validator->startContext();
  89.     }
  90.     public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
  91.     {
  92.         return $this->validator->inContext($context);
  93.     }
  94. }