Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\Validator\DataCollector; Chris@14: Chris@14: use Symfony\Component\Form\FormInterface; Chris@14: use Symfony\Component\HttpFoundation\Request; Chris@14: use Symfony\Component\HttpFoundation\Response; Chris@14: use Symfony\Component\HttpKernel\DataCollector\DataCollector; Chris@14: use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; Chris@14: use Symfony\Component\Validator\Validator\TraceableValidator; Chris@14: use Symfony\Component\VarDumper\Caster\Caster; Chris@14: use Symfony\Component\VarDumper\Caster\ClassStub; Chris@14: use Symfony\Component\VarDumper\Cloner\Data; Chris@14: use Symfony\Component\VarDumper\Cloner\Stub; Chris@14: Chris@14: /** Chris@14: * @author Maxime Steinhausser Chris@14: */ Chris@14: class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface Chris@14: { Chris@14: private $validator; Chris@14: Chris@14: public function __construct(TraceableValidator $validator) Chris@14: { Chris@14: $this->validator = $validator; Chris@14: $this->reset(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function collect(Request $request, Response $response, \Exception $exception = null) Chris@14: { Chris@14: // Everything is collected once, on kernel terminate. Chris@14: } Chris@14: Chris@14: public function reset() Chris@14: { Chris@17: $this->data = [ Chris@17: 'calls' => $this->cloneVar([]), Chris@14: 'violations_count' => 0, Chris@17: ]; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function lateCollect() Chris@14: { Chris@14: $collected = $this->validator->getCollectedData(); Chris@14: $this->data['calls'] = $this->cloneVar($collected); Chris@14: $this->data['violations_count'] = array_reduce($collected, function ($previous, $item) { Chris@17: return $previous + \count($item['violations']); Chris@14: }, 0); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @return Data Chris@14: */ Chris@14: public function getCalls() Chris@14: { Chris@14: return $this->data['calls']; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @return int Chris@14: */ Chris@14: public function getViolationsCount() Chris@14: { Chris@14: return $this->data['violations_count']; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function getName() Chris@14: { Chris@14: return 'validator'; Chris@14: } Chris@14: Chris@14: protected function getCasters() Chris@14: { Chris@17: return parent::getCasters() + [ Chris@14: \Exception::class => function (\Exception $e, array $a, Stub $s) { Chris@17: foreach (["\0Exception\0previous", "\0Exception\0trace"] as $k) { Chris@14: if (isset($a[$k])) { Chris@14: unset($a[$k]); Chris@14: ++$s->cut; Chris@14: } Chris@14: } Chris@14: Chris@14: return $a; Chris@14: }, Chris@14: FormInterface::class => function (FormInterface $f, array $a) { Chris@17: return [ Chris@14: Caster::PREFIX_VIRTUAL.'name' => $f->getName(), Chris@17: Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())), Chris@14: Caster::PREFIX_VIRTUAL.'data' => $f->getData(), Chris@17: ]; Chris@14: }, Chris@17: ]; Chris@14: } Chris@14: }