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

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