Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Validator / RecursiveValidator.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\ConstraintValidatorFactoryInterface;
15 use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
16 use Symfony\Component\Validator\Context\ExecutionContextInterface;
17 use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
18 use Symfony\Component\Validator\ObjectInitializerInterface;
19
20 /**
21  * Recursive implementation of {@link ValidatorInterface}.
22  *
23  * @author Bernhard Schussek <bschussek@gmail.com>
24  */
25 class RecursiveValidator implements ValidatorInterface
26 {
27     /**
28      * @var ExecutionContextFactoryInterface
29      */
30     protected $contextFactory;
31
32     /**
33      * @var MetadataFactoryInterface
34      */
35     protected $metadataFactory;
36
37     /**
38      * @var ConstraintValidatorFactoryInterface
39      */
40     protected $validatorFactory;
41
42     /**
43      * @var ObjectInitializerInterface[]
44      */
45     protected $objectInitializers;
46
47     /**
48      * Creates a new validator.
49      *
50      * @param ExecutionContextFactoryInterface    $contextFactory     The factory for
51      *                                                                creating new contexts
52      * @param MetadataFactoryInterface            $metadataFactory    The factory for
53      *                                                                fetching the metadata
54      *                                                                of validated objects
55      * @param ConstraintValidatorFactoryInterface $validatorFactory   The factory for creating
56      *                                                                constraint validators
57      * @param ObjectInitializerInterface[]        $objectInitializers The object initializers
58      */
59     public function __construct(ExecutionContextFactoryInterface $contextFactory, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = array())
60     {
61         $this->contextFactory = $contextFactory;
62         $this->metadataFactory = $metadataFactory;
63         $this->validatorFactory = $validatorFactory;
64         $this->objectInitializers = $objectInitializers;
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     public function startContext($root = null)
71     {
72         return new RecursiveContextualValidator(
73             $this->contextFactory->createContext($this, $root),
74             $this->metadataFactory,
75             $this->validatorFactory,
76             $this->objectInitializers
77         );
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function inContext(ExecutionContextInterface $context)
84     {
85         return new RecursiveContextualValidator(
86             $context,
87             $this->metadataFactory,
88             $this->validatorFactory,
89             $this->objectInitializers
90         );
91     }
92
93     /**
94      * {@inheritdoc}
95      */
96     public function getMetadataFor($object)
97     {
98         return $this->metadataFactory->getMetadataFor($object);
99     }
100
101     /**
102      * {@inheritdoc}
103      */
104     public function hasMetadataFor($object)
105     {
106         return $this->metadataFactory->hasMetadataFor($object);
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     public function validate($value, $constraints = null, $groups = null)
113     {
114         return $this->startContext($value)
115             ->validate($value, $constraints, $groups)
116             ->getViolations();
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     public function validateProperty($object, $propertyName, $groups = null)
123     {
124         return $this->startContext($object)
125             ->validateProperty($object, $propertyName, $groups)
126             ->getViolations();
127     }
128
129     /**
130      * {@inheritdoc}
131      */
132     public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
133     {
134         // If a class name is passed, take $value as root
135         return $this->startContext(is_object($objectOrClass) ? $objectOrClass : $value)
136             ->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
137             ->getViolations();
138     }
139 }