forked from symfony/validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
232 lines (197 loc) · 6.86 KB
/
Validator.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
<?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;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\ValidatorException;
/**
* Default implementation of {@link ValidatorInterface}.
*
* @author Fabien Potencier <[email protected]>
* @author Bernhard Schussek <[email protected]>
*
* @deprecated Deprecated since version 2.5, to be removed in Symfony 3.0.
* Use {@link Validator\RecursiveValidator} instead.
*/
class Validator implements ValidatorInterface, Mapping\Factory\MetadataFactoryInterface
{
/**
* @var MetadataFactoryInterface
*/
private $metadataFactory;
/**
* @var ConstraintValidatorFactoryInterface
*/
private $validatorFactory;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var null|string
*/
private $translationDomain;
/**
* @var array
*/
private $objectInitializers;
public function __construct(
MetadataFactoryInterface $metadataFactory,
ConstraintValidatorFactoryInterface $validatorFactory,
TranslatorInterface $translator,
$translationDomain = 'validators',
array $objectInitializers = array()
)
{
$this->metadataFactory = $metadataFactory;
$this->validatorFactory = $validatorFactory;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->objectInitializers = $objectInitializers;
}
/**
* {@inheritdoc}
*/
public function getMetadataFactory()
{
return $this->metadataFactory;
}
/**
* {@inheritDoc}
*/
public function getMetadataFor($value)
{
return $this->metadataFactory->getMetadataFor($value);
}
/**
* {@inheritDoc}
*/
public function hasMetadataFor($value)
{
return $this->metadataFactory->hasMetadataFor($value);
}
/**
* {@inheritDoc}
*/
public function validate($value, $groups = null, $traverse = false, $deep = false)
{
$visitor = $this->createVisitor($value);
foreach ($this->resolveGroups($groups) as $group) {
$visitor->validate($value, $group, '', $traverse, $deep);
}
return $visitor->getViolations();
}
/**
* {@inheritDoc}
*
* @throws ValidatorException If the metadata for the value does not support properties.
*/
public function validateProperty($containingValue, $property, $groups = null)
{
$visitor = $this->createVisitor($containingValue);
$metadata = $this->metadataFactory->getMetadataFor($containingValue);
if (!$metadata instanceof PropertyMetadataContainerInterface) {
$valueAsString = is_scalar($containingValue)
? '"'.$containingValue.'"'
: 'the value of type '.gettype($containingValue);
throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
}
foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
}
}
return $visitor->getViolations();
}
/**
* {@inheritDoc}
*
* @throws ValidatorException If the metadata for the value does not support properties.
*/
public function validatePropertyValue($containingValue, $property, $value, $groups = null)
{
$visitor = $this->createVisitor($containingValue);
$metadata = $this->metadataFactory->getMetadataFor($containingValue);
if (!$metadata instanceof PropertyMetadataContainerInterface) {
$valueAsString = is_scalar($containingValue)
? '"'.$containingValue.'"'
: 'the value of type '.gettype($containingValue);
throw new ValidatorException(sprintf('The metadata for '.$valueAsString.' does not support properties.'));
}
foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $value, $group, $property);
}
}
return $visitor->getViolations();
}
/**
* {@inheritDoc}
*/
public function validateValue($value, $constraints, $groups = null)
{
$context = new ExecutionContext($this->createVisitor($value), $this->translator, $this->translationDomain);
$constraints = is_array($constraints) ? $constraints : array($constraints);
foreach ($constraints as $constraint) {
if ($constraint instanceof Valid) {
// Why can't the Valid constraint be executed directly?
//
// It cannot be executed like regular other constraints, because regular
// constraints are only executed *if they belong to the validated group*.
// The Valid constraint, on the other hand, is always executed and propagates
// the group to the cascaded object. The propagated group depends on
//
// * Whether a group sequence is currently being executed. Then the default
// group is propagated.
//
// * Otherwise the validated group is propagated.
throw new ValidatorException(
sprintf(
'The constraint %s cannot be validated. Use the method validate() instead.',
get_class($constraint)
)
);
}
$context->validateValue($value, $constraint, '', $groups);
}
return $context->getViolations();
}
/**
* @param mixed $root
*
* @return ValidationVisitor
*/
private function createVisitor($root)
{
return new ValidationVisitor(
$root,
$this->metadataFactory,
$this->validatorFactory,
$this->translator,
$this->translationDomain,
$this->objectInitializers
);
}
/**
* @param null|string|string[] $groups
*
* @return string[]
*/
private function resolveGroups($groups)
{
return $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP);
}
}