ValidatorDataCollector.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 $validator;
  29. public function __construct(TraceableValidator $validator)
  30. {
  31. $this->validator = $validator;
  32. $this->reset();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function collect(Request $request, Response $response, \Throwable $exception = null)
  38. {
  39. // Everything is collected once, on kernel terminate.
  40. }
  41. public function reset()
  42. {
  43. $this->data = [
  44. 'calls' => $this->cloneVar([]),
  45. 'violations_count' => 0,
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function lateCollect()
  52. {
  53. $collected = $this->validator->getCollectedData();
  54. $this->data['calls'] = $this->cloneVar($collected);
  55. $this->data['violations_count'] = array_reduce($collected, function ($previous, $item) {
  56. return $previous + \count($item['violations']);
  57. }, 0);
  58. }
  59. /**
  60. * @return Data
  61. */
  62. public function getCalls()
  63. {
  64. return $this->data['calls'];
  65. }
  66. /**
  67. * @return int
  68. */
  69. public function getViolationsCount()
  70. {
  71. return $this->data['violations_count'];
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getName()
  77. {
  78. return 'validator';
  79. }
  80. protected function getCasters()
  81. {
  82. return parent::getCasters() + [
  83. \Exception::class => function (\Exception $e, array $a, Stub $s) {
  84. foreach (["\0Exception\0previous", "\0Exception\0trace"] as $k) {
  85. if (isset($a[$k])) {
  86. unset($a[$k]);
  87. ++$s->cut;
  88. }
  89. }
  90. return $a;
  91. },
  92. FormInterface::class => function (FormInterface $f, array $a) {
  93. return [
  94. Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
  95. Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
  96. Caster::PREFIX_VIRTUAL.'data' => $f->getData(),
  97. ];
  98. },
  99. ];
  100. }
  101. }