Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Constraints / CallbackValidator.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\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * Validator for Callback constraint.
21  *
22  * @author Bernhard Schussek <bschussek@gmail.com>
23  */
24 class CallbackValidator extends ConstraintValidator
25 {
26     /**
27      * {@inheritdoc}
28      */
29     public function validate($object, Constraint $constraint)
30     {
31         if (!$constraint instanceof Callback) {
32             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
33         }
34
35         $method = $constraint->callback;
36         if ($method instanceof \Closure) {
37             $method($object, $this->context, $constraint->payload);
38         } elseif (is_array($method)) {
39             if (!is_callable($method)) {
40                 if (isset($method[0]) && is_object($method[0])) {
41                     $method[0] = get_class($method[0]);
42                 }
43                 throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
44             }
45
46             call_user_func($method, $object, $this->context, $constraint->payload);
47         } elseif (null !== $object) {
48             if (!method_exists($object, $method)) {
49                 throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
50             }
51
52             $reflMethod = new \ReflectionMethod($object, $method);
53
54             if ($reflMethod->isStatic()) {
55                 $reflMethod->invoke(null, $object, $this->context, $constraint->payload);
56             } else {
57                 $reflMethod->invoke($object, $this->context, $constraint->payload);
58             }
59         }
60     }
61 }