9c50a4f0f685b51a1d0ca3f79b1b66f77470f374
[yaffs-website] / vendor / symfony / validator / Context / ExecutionContext.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\Context;
13
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintViolation;
17 use Symfony\Component\Validator\ConstraintViolationList;
18 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
19 use Symfony\Component\Validator\Mapping\MetadataInterface;
20 use Symfony\Component\Validator\Mapping\MemberMetadata;
21 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
22 use Symfony\Component\Validator\Util\PropertyPath;
23 use Symfony\Component\Validator\Validator\ValidatorInterface;
24 use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
25
26 /**
27  * The context used and created by {@link ExecutionContextFactory}.
28  *
29  * @author Bernhard Schussek <bschussek@gmail.com>
30  *
31  * @see ExecutionContextInterface
32  *
33  * @internal since version 2.5. Code against ExecutionContextInterface instead.
34  */
35 class ExecutionContext implements ExecutionContextInterface
36 {
37     /**
38      * @var ValidatorInterface
39      */
40     private $validator;
41
42     /**
43      * The root value of the validated object graph.
44      *
45      * @var mixed
46      */
47     private $root;
48
49     /**
50      * @var TranslatorInterface
51      */
52     private $translator;
53
54     /**
55      * @var string
56      */
57     private $translationDomain;
58
59     /**
60      * The violations generated in the current context.
61      *
62      * @var ConstraintViolationList
63      */
64     private $violations;
65
66     /**
67      * The currently validated value.
68      *
69      * @var mixed
70      */
71     private $value;
72
73     /**
74      * The currently validated object.
75      *
76      * @var object|null
77      */
78     private $object;
79
80     /**
81      * The property path leading to the current value.
82      *
83      * @var string
84      */
85     private $propertyPath = '';
86
87     /**
88      * The current validation metadata.
89      *
90      * @var MetadataInterface|null
91      */
92     private $metadata;
93
94     /**
95      * The currently validated group.
96      *
97      * @var string|null
98      */
99     private $group;
100
101     /**
102      * The currently validated constraint.
103      *
104      * @var Constraint|null
105      */
106     private $constraint;
107
108     /**
109      * Stores which objects have been validated in which group.
110      *
111      * @var array
112      */
113     private $validatedObjects = array();
114
115     /**
116      * Stores which class constraint has been validated for which object.
117      *
118      * @var array
119      */
120     private $validatedConstraints = array();
121
122     /**
123      * Stores which objects have been initialized.
124      *
125      * @var array
126      */
127     private $initializedObjects;
128
129     /**
130      * Creates a new execution context.
131      *
132      * @param ValidatorInterface  $validator         The validator
133      * @param mixed               $root              The root value of the
134      *                                               validated object graph
135      * @param TranslatorInterface $translator        The translator
136      * @param string|null         $translationDomain The translation domain to
137      *                                               use for translating
138      *                                               violation messages
139      *
140      * @internal Called by {@link ExecutionContextFactory}. Should not be used
141      *           in user code.
142      */
143     public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
144     {
145         $this->validator = $validator;
146         $this->root = $root;
147         $this->translator = $translator;
148         $this->translationDomain = $translationDomain;
149         $this->violations = new ConstraintViolationList();
150     }
151
152     /**
153      * {@inheritdoc}
154      */
155     public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
156     {
157         $this->value = $value;
158         $this->object = $object;
159         $this->metadata = $metadata;
160         $this->propertyPath = (string) $propertyPath;
161     }
162
163     /**
164      * {@inheritdoc}
165      */
166     public function setGroup($group)
167     {
168         $this->group = $group;
169     }
170
171     /**
172      * {@inheritdoc}
173      */
174     public function setConstraint(Constraint $constraint)
175     {
176         $this->constraint = $constraint;
177     }
178
179     /**
180      * {@inheritdoc}
181      */
182     public function addViolation($message, array $parameters = array())
183     {
184         $this->violations->add(new ConstraintViolation(
185             $this->translator->trans($message, $parameters, $this->translationDomain),
186             $message,
187             $parameters,
188             $this->root,
189             $this->propertyPath,
190             $this->value,
191             null,
192             null,
193             $this->constraint
194         ));
195     }
196
197     /**
198      * {@inheritdoc}
199      */
200     public function buildViolation($message, array $parameters = array())
201     {
202         return new ConstraintViolationBuilder(
203             $this->violations,
204             $this->constraint,
205             $message,
206             $parameters,
207             $this->root,
208             $this->propertyPath,
209             $this->value,
210             $this->translator,
211             $this->translationDomain
212         );
213     }
214
215     /**
216      * {@inheritdoc}
217      */
218     public function getViolations()
219     {
220         return $this->violations;
221     }
222
223     /**
224      * {@inheritdoc}
225      */
226     public function getValidator()
227     {
228         return $this->validator;
229     }
230
231     /**
232      * {@inheritdoc}
233      */
234     public function getRoot()
235     {
236         return $this->root;
237     }
238
239     /**
240      * {@inheritdoc}
241      */
242     public function getValue()
243     {
244         return $this->value;
245     }
246
247     /**
248      * {@inheritdoc}
249      */
250     public function getObject()
251     {
252         return $this->object;
253     }
254
255     /**
256      * {@inheritdoc}
257      */
258     public function getMetadata()
259     {
260         return $this->metadata;
261     }
262
263     /**
264      * {@inheritdoc}
265      */
266     public function getGroup()
267     {
268         return $this->group;
269     }
270
271     public function getConstraint()
272     {
273         return $this->constraint;
274     }
275
276     /**
277      * {@inheritdoc}
278      */
279     public function getClassName()
280     {
281         return $this->metadata instanceof MemberMetadata || $this->metadata instanceof ClassMetadataInterface ? $this->metadata->getClassName() : null;
282     }
283
284     /**
285      * {@inheritdoc}
286      */
287     public function getPropertyName()
288     {
289         return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
290     }
291
292     /**
293      * {@inheritdoc}
294      */
295     public function getPropertyPath($subPath = '')
296     {
297         return PropertyPath::append($this->propertyPath, $subPath);
298     }
299
300     /**
301      * {@inheritdoc}
302      */
303     public function markGroupAsValidated($cacheKey, $groupHash)
304     {
305         if (!isset($this->validatedObjects[$cacheKey])) {
306             $this->validatedObjects[$cacheKey] = array();
307         }
308
309         $this->validatedObjects[$cacheKey][$groupHash] = true;
310     }
311
312     /**
313      * {@inheritdoc}
314      */
315     public function isGroupValidated($cacheKey, $groupHash)
316     {
317         return isset($this->validatedObjects[$cacheKey][$groupHash]);
318     }
319
320     /**
321      * {@inheritdoc}
322      */
323     public function markConstraintAsValidated($cacheKey, $constraintHash)
324     {
325         $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
326     }
327
328     /**
329      * {@inheritdoc}
330      */
331     public function isConstraintValidated($cacheKey, $constraintHash)
332     {
333         return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
334     }
335
336     /**
337      * {@inheritdoc}
338      */
339     public function markObjectAsInitialized($cacheKey)
340     {
341         $this->initializedObjects[$cacheKey] = true;
342     }
343
344     /**
345      * {@inheritdoc}
346      */
347     public function isObjectInitialized($cacheKey)
348     {
349         return isset($this->initializedObjects[$cacheKey]);
350     }
351 }