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\Validator;
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
17 * Collects some data about validator calls.
19 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
21 class TraceableValidator implements ValidatorInterface
24 private $collectedData = array();
26 public function __construct(ValidatorInterface $validator)
28 $this->validator = $validator;
34 public function getCollectedData()
36 return $this->collectedData;
39 public function reset()
41 $this->collectedData = array();
47 public function getMetadataFor($value)
49 return $this->validator->getMetadataFor($value);
55 public function hasMetadataFor($value)
57 return $this->validator->hasMetadataFor($value);
63 public function validate($value, $constraints = null, $groups = null)
65 $violations = $this->validator->validate($value, $constraints, $groups);
67 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
69 $file = $trace[0]['file'];
70 $line = $trace[0]['line'];
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)
77 $file = $trace[$i]['file'];
78 $line = $trace[$i]['line'];
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'];
92 $name = str_replace('\\', '/', $file);
93 $name = substr($name, strrpos($name, '/') + 1);
95 $this->collectedData[] = array(
96 'caller' => compact('name', 'file', 'line'),
97 'context' => compact('value', 'constraints', 'groups'),
98 'violations' => iterator_to_array($violations),
107 public function validateProperty($object, $propertyName, $groups = null)
109 return $this->validator->validateProperty($object, $propertyName, $groups);
115 public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
117 return $this->validator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
123 public function startContext()
125 return $this->validator->startContext();
131 public function inContext(ExecutionContextInterface $context)
133 return $this->validator->inContext($context);