Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\ConstraintValidator;
|
Chris@0
|
16 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Validator for Callback constraint.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class CallbackValidator extends ConstraintValidator
|
Chris@0
|
25 {
|
Chris@0
|
26 /**
|
Chris@0
|
27 * {@inheritdoc}
|
Chris@0
|
28 */
|
Chris@0
|
29 public function validate($object, Constraint $constraint)
|
Chris@0
|
30 {
|
Chris@0
|
31 if (!$constraint instanceof Callback) {
|
Chris@0
|
32 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 $method = $constraint->callback;
|
Chris@0
|
36 if ($method instanceof \Closure) {
|
Chris@0
|
37 $method($object, $this->context, $constraint->payload);
|
Chris@17
|
38 } elseif (\is_array($method)) {
|
Chris@17
|
39 if (!\is_callable($method)) {
|
Chris@17
|
40 if (isset($method[0]) && \is_object($method[0])) {
|
Chris@17
|
41 $method[0] = \get_class($method[0]);
|
Chris@0
|
42 }
|
Chris@0
|
43 throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@17
|
46 \call_user_func($method, $object, $this->context, $constraint->payload);
|
Chris@0
|
47 } elseif (null !== $object) {
|
Chris@0
|
48 if (!method_exists($object, $method)) {
|
Chris@17
|
49 throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, \get_class($object)));
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 $reflMethod = new \ReflectionMethod($object, $method);
|
Chris@0
|
53
|
Chris@0
|
54 if ($reflMethod->isStatic()) {
|
Chris@0
|
55 $reflMethod->invoke(null, $object, $this->context, $constraint->payload);
|
Chris@0
|
56 } else {
|
Chris@0
|
57 $reflMethod->invoke($object, $this->context, $constraint->payload);
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|