Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / validator / DataCollector / ValidatorDataCollector.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\DataCollector;
13
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;
24
25 /**
26  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
27  */
28 class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface
29 {
30     private $validator;
31
32     public function __construct(TraceableValidator $validator)
33     {
34         $this->validator = $validator;
35         $this->reset();
36     }
37
38     /**
39      * {@inheritdoc}
40      */
41     public function collect(Request $request, Response $response, \Exception $exception = null)
42     {
43         // Everything is collected once, on kernel terminate.
44     }
45
46     public function reset()
47     {
48         $this->data = array(
49             'calls' => $this->cloneVar(array()),
50             'violations_count' => 0,
51         );
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     public function lateCollect()
58     {
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']);
63         }, 0);
64     }
65
66     /**
67      * @return Data
68      */
69     public function getCalls()
70     {
71         return $this->data['calls'];
72     }
73
74     /**
75      * @return int
76      */
77     public function getViolationsCount()
78     {
79         return $this->data['violations_count'];
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     public function getName()
86     {
87         return 'validator';
88     }
89
90     protected function getCasters()
91     {
92         return parent::getCasters() + array(
93             \Exception::class => function (\Exception $e, array $a, Stub $s) {
94                 foreach (array("\0Exception\0previous", "\0Exception\0trace") as $k) {
95                     if (isset($a[$k])) {
96                         unset($a[$k]);
97                         ++$s->cut;
98                     }
99                 }
100
101                 return $a;
102             },
103             FormInterface::class => function (FormInterface $f, array $a) {
104                 return array(
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(),
108                 );
109             },
110         );
111     }
112 }