96134e276746898a9b2740a93e5025daf80913c4
[yaffs-website] / validator / Validator / TraceableValidator.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\Validator;
13
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
15
16 /**
17  * Collects some data about validator calls.
18  *
19  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
20  */
21 class TraceableValidator implements ValidatorInterface
22 {
23     private $validator;
24     private $collectedData = array();
25
26     public function __construct(ValidatorInterface $validator)
27     {
28         $this->validator = $validator;
29     }
30
31     /**
32      * @return array
33      */
34     public function getCollectedData()
35     {
36         return $this->collectedData;
37     }
38
39     public function reset()
40     {
41         $this->collectedData = array();
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     public function getMetadataFor($value)
48     {
49         return $this->validator->getMetadataFor($value);
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function hasMetadataFor($value)
56     {
57         return $this->validator->hasMetadataFor($value);
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function validate($value, $constraints = null, $groups = null)
64     {
65         $violations = $this->validator->validate($value, $constraints, $groups);
66
67         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
68
69         $file = $trace[0]['file'];
70         $line = $trace[0]['line'];
71
72         for ($i = 1; $i < 7; ++$i) {
73             if (isset($trace[$i]['class'], $trace[$i]['function'])
74                 && 'validate' === $trace[$i]['function']
75                 && is_a($trace[$i]['class'], ValidatorInterface::class, true)
76             ) {
77                 $file = $trace[$i]['file'];
78                 $line = $trace[$i]['line'];
79
80                 while (++$i < 7) {
81                     if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
82                         $file = $trace[$i]['file'];
83                         $line = $trace[$i]['line'];
84
85                         break;
86                     }
87                 }
88                 break;
89             }
90         }
91
92         $name = str_replace('\\', '/', $file);
93         $name = substr($name, strrpos($name, '/') + 1);
94
95         $this->collectedData[] = array(
96             'caller' => compact('name', 'file', 'line'),
97             'context' => compact('value', 'constraints', 'groups'),
98             'violations' => iterator_to_array($violations),
99         );
100
101         return $violations;
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     public function validateProperty($object, $propertyName, $groups = null)
108     {
109         return $this->validator->validateProperty($object, $propertyName, $groups);
110     }
111
112     /**
113      * {@inheritdoc}
114      */
115     public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
116     {
117         return $this->validator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     public function startContext()
124     {
125         return $this->validator->startContext();
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     public function inContext(ExecutionContextInterface $context)
132     {
133         return $this->validator->inContext($context);
134     }
135 }