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