vendor/symfony/validator/DataCollector/ValidatorDataCollector.php line 40

  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\DataCollector;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  15. use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  16. use Symfony\Component\Validator\Validator\TraceableValidator;
  17. use Symfony\Component\VarDumper\Caster\Caster;
  18. use Symfony\Component\VarDumper\Caster\ClassStub;
  19. use Symfony\Component\VarDumper\Cloner\Data;
  20. use Symfony\Component\VarDumper\Cloner\Stub;
  21. /**
  22.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  23.  *
  24.  * @final
  25.  */
  26. class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface
  27. {
  28.     private TraceableValidator $validator;
  29.     public function __construct(TraceableValidator $validator)
  30.     {
  31.         $this->validator $validator;
  32.         $this->reset();
  33.     }
  34.     public function collect(Request $requestResponse $response\Throwable $exception null)
  35.     {
  36.         // Everything is collected once, on kernel terminate.
  37.     }
  38.     public function reset()
  39.     {
  40.         $this->data = [
  41.             'calls' => $this->cloneVar([]),
  42.             'violations_count' => 0,
  43.         ];
  44.     }
  45.     public function lateCollect()
  46.     {
  47.         $collected $this->validator->getCollectedData();
  48.         $this->data['calls'] = $this->cloneVar($collected);
  49.         $this->data['violations_count'] = array_reduce($collected, function ($previous$item) {
  50.             return $previous \count($item['violations']);
  51.         }, 0);
  52.     }
  53.     public function getCalls(): Data
  54.     {
  55.         return $this->data['calls'];
  56.     }
  57.     public function getViolationsCount(): int
  58.     {
  59.         return $this->data['violations_count'];
  60.     }
  61.     public function getName(): string
  62.     {
  63.         return 'validator';
  64.     }
  65.     protected function getCasters(): array
  66.     {
  67.         return parent::getCasters() + [
  68.             \Exception::class => function (\Exception $e, array $aStub $s) {
  69.                 foreach (["\0Exception\0previous""\0Exception\0trace"] as $k) {
  70.                     if (isset($a[$k])) {
  71.                         unset($a[$k]);
  72.                         ++$s->cut;
  73.                     }
  74.                 }
  75.                 return $a;
  76.             },
  77.             FormInterface::class => function (FormInterface $f, array $a) {
  78.                 return [
  79.                     Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
  80.                     Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
  81.                     Caster::PREFIX_VIRTUAL.'data' => $f->getData(),
  82.                 ];
  83.             },
  84.         ];
  85.     }
  86. }