4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\DataCollector;
14 use Symfony\Component\Form\FormInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
18 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
19 use Symfony\Component\Validator\Validator\TraceableValidator;
20 use Symfony\Component\VarDumper\Caster\Caster;
21 use Symfony\Component\VarDumper\Caster\ClassStub;
22 use Symfony\Component\VarDumper\Cloner\Data;
23 use Symfony\Component\VarDumper\Cloner\Stub;
26 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
28 class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface
32 public function __construct(TraceableValidator $validator)
34 $this->validator = $validator;
41 public function collect(Request $request, Response $response, \Exception $exception = null)
43 // Everything is collected once, on kernel terminate.
46 public function reset()
49 'calls' => $this->cloneVar(array()),
50 'violations_count' => 0,
57 public function lateCollect()
59 $collected = $this->validator->getCollectedData();
60 $this->data['calls'] = $this->cloneVar($collected);
61 $this->data['violations_count'] = array_reduce($collected, function ($previous, $item) {
62 return $previous + \count($item['violations']);
69 public function getCalls()
71 return $this->data['calls'];
77 public function getViolationsCount()
79 return $this->data['violations_count'];
85 public function getName()
90 protected function getCasters()
92 return parent::getCasters() + array(
93 \Exception::class => function (\Exception $e, array $a, Stub $s) {
94 foreach (array("\0Exception\0previous", "\0Exception\0trace") as $k) {
103 FormInterface::class => function (FormInterface $f, array $a) {
105 Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
106 Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
107 Caster::PREFIX_VIRTUAL.'data' => $f->getData(),