Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / symfony / validator / 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;
13
14 @trigger_error('The '.__NAMESPACE__.'\ExecutionContext class is deprecated since version 2.5 and will be removed in 3.0. Use the Symfony\Component\Validator\Context\ExecutionContext class instead.', E_USER_DEPRECATED);
15
16 use Symfony\Component\Translation\TranslatorInterface;
17
18 /**
19  * Default implementation of {@link ExecutionContextInterface}.
20  *
21  * This class is immutable by design.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  * @author Bernhard Schussek <bschussek@gmail.com>
25  *
26  * @deprecated since version 2.5, to be removed in 3.0.
27  *             Use {@link Context\ExecutionContext} instead.
28  */
29 class ExecutionContext implements ExecutionContextInterface
30 {
31     /**
32      * @var GlobalExecutionContextInterface
33      */
34     private $globalContext;
35
36     /**
37      * @var TranslatorInterface
38      */
39     private $translator;
40
41     /**
42      * @var null|string
43      */
44     private $translationDomain;
45
46     /**
47      * @var MetadataInterface
48      */
49     private $metadata;
50
51     /**
52      * @var mixed
53      */
54     private $value;
55
56     /**
57      * @var string
58      */
59     private $group;
60
61     /**
62      * @var string
63      */
64     private $propertyPath;
65
66     /**
67      * Creates a new execution context.
68      *
69      * @param GlobalExecutionContextInterface $globalContext     The global context storing node-independent state
70      * @param TranslatorInterface             $translator        The translator for translating violation messages
71      * @param null|string                     $translationDomain The domain of the validation messages
72      * @param MetadataInterface               $metadata          The metadata of the validated node
73      * @param mixed                           $value             The value of the validated node
74      * @param string                          $group             The current validation group
75      * @param string                          $propertyPath      The property path to the current node
76      */
77     public function __construct(GlobalExecutionContextInterface $globalContext, TranslatorInterface $translator, $translationDomain = null, MetadataInterface $metadata = null, $value = null, $group = null, $propertyPath = '')
78     {
79         if (null === $group) {
80             $group = Constraint::DEFAULT_GROUP;
81         }
82
83         $this->globalContext = $globalContext;
84         $this->translator = $translator;
85         $this->translationDomain = $translationDomain;
86         $this->metadata = $metadata;
87         $this->value = $value;
88         $this->propertyPath = $propertyPath;
89         $this->group = $group;
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function addViolation($message, array $params = array(), $invalidValue = null, $plural = null, $code = null)
96     {
97         if (null === $plural) {
98             $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
99         } else {
100             try {
101                 $translatedMessage = $this->translator->transChoice($message, $plural, $params, $this->translationDomain);
102             } catch (\InvalidArgumentException $e) {
103                 $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
104             }
105         }
106
107         $this->globalContext->getViolations()->add(new ConstraintViolation(
108             $translatedMessage,
109             $message,
110             $params,
111             $this->globalContext->getRoot(),
112             $this->propertyPath,
113             // check using func_num_args() to allow passing null values
114             func_num_args() >= 3 ? $invalidValue : $this->value,
115             $plural,
116             $code
117         ));
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
124     {
125         $this->globalContext->getViolations()->add(new ConstraintViolation(
126             null === $plural
127                 ? $this->translator->trans($message, $parameters, $this->translationDomain)
128                 : $this->translator->transChoice($message, $plural, $parameters, $this->translationDomain),
129             $message,
130             $parameters,
131             $this->globalContext->getRoot(),
132             $this->getPropertyPath($subPath),
133             // check using func_num_args() to allow passing null values
134             func_num_args() >= 4 ? $invalidValue : $this->value,
135             $plural,
136             $code
137         ));
138     }
139
140     /**
141      * {@inheritdoc}
142      */
143     public function getViolations()
144     {
145         return $this->globalContext->getViolations();
146     }
147
148     /**
149      * {@inheritdoc}
150      */
151     public function getRoot()
152     {
153         return $this->globalContext->getRoot();
154     }
155
156     /**
157      * {@inheritdoc}
158      */
159     public function getPropertyPath($subPath = '')
160     {
161         if ('' != $subPath && '' !== $this->propertyPath && '[' !== $subPath[0]) {
162             return $this->propertyPath.'.'.$subPath;
163         }
164
165         return $this->propertyPath.$subPath;
166     }
167
168     /**
169      * {@inheritdoc}
170      */
171     public function getClassName()
172     {
173         if ($this->metadata instanceof ClassBasedInterface) {
174             return $this->metadata->getClassName();
175         }
176     }
177
178     /**
179      * {@inheritdoc}
180      */
181     public function getPropertyName()
182     {
183         if ($this->metadata instanceof PropertyMetadataInterface) {
184             return $this->metadata->getPropertyName();
185         }
186     }
187
188     /**
189      * {@inheritdoc}
190      */
191     public function getValue()
192     {
193         return $this->value;
194     }
195
196     /**
197      * {@inheritdoc}
198      */
199     public function getGroup()
200     {
201         return $this->group;
202     }
203
204     /**
205      * {@inheritdoc}
206      */
207     public function getMetadata()
208     {
209         return $this->metadata;
210     }
211
212     /**
213      * {@inheritdoc}
214      */
215     public function getMetadataFor($value)
216     {
217         return $this->globalContext->getMetadataFactory()->getMetadataFor($value);
218     }
219
220     /**
221      * {@inheritdoc}
222      */
223     public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
224     {
225         $propertyPath = $this->getPropertyPath($subPath);
226
227         foreach ($this->resolveGroups($groups) as $group) {
228             $this->globalContext->getVisitor()->validate($value, $group, $propertyPath, $traverse, $deep);
229         }
230     }
231
232     /**
233      * {@inheritdoc}
234      */
235     public function validateValue($value, $constraints, $subPath = '', $groups = null)
236     {
237         $constraints = is_array($constraints) ? $constraints : array($constraints);
238
239         if (null === $groups && '' === $subPath) {
240             $context = clone $this;
241             $context->value = $value;
242             $context->executeConstraintValidators($value, $constraints);
243
244             return;
245         }
246
247         $propertyPath = $this->getPropertyPath($subPath);
248
249         foreach ($this->resolveGroups($groups) as $group) {
250             $context = clone $this;
251             $context->value = $value;
252             $context->group = $group;
253             $context->propertyPath = $propertyPath;
254             $context->executeConstraintValidators($value, $constraints);
255         }
256     }
257
258     /**
259      * {@inheritdoc}
260      */
261     public function getMetadataFactory()
262     {
263         return $this->globalContext->getMetadataFactory();
264     }
265
266     /**
267      * Executes the validators of the given constraints for the given value.
268      *
269      * @param mixed        $value       The value to validate
270      * @param Constraint[] $constraints The constraints to match against
271      */
272     private function executeConstraintValidators($value, array $constraints)
273     {
274         foreach ($constraints as $constraint) {
275             $validator = $this->globalContext->getValidatorFactory()->getInstance($constraint);
276             $validator->initialize($this);
277             $validator->validate($value, $constraint);
278         }
279     }
280
281     /**
282      * Returns an array of group names.
283      *
284      * @param null|string|string[] $groups The groups to resolve. If a single string is
285      *                                     passed, it is converted to an array. If null
286      *                                     is passed, an array containing the current
287      *                                     group of the context is returned.
288      *
289      * @return array An array of validation groups
290      */
291     private function resolveGroups($groups)
292     {
293         return $groups ? (array) $groups : (array) $this->group;
294     }
295 }