forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormDataConstraintFinder.php
322 lines (273 loc) · 11 KB
/
FormDataConstraintFinder.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace Boekkooi\Bundle\JqueryValidationBundle\Form;
use Boekkooi\Bundle\JqueryValidationBundle\Exception\UnsupportedException;
use Boekkooi\Bundle\JqueryValidationBundle\Validator\ConstraintCollection;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\CascadingStrategy;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\MemberMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class FormDataConstraintFinder
{
/**
* @var MetadataFactoryInterface
*/
private $metadataFactory;
/**
* Constructor.
* @param MetadataFactoryInterface $metadataFactory
*/
public function __construct($metadataFactory)
{
if (
!$metadataFactory instanceof MetadataFactoryInterface &&
!$metadataFactory instanceof \Symfony\Component\Validator\MetadataFactoryInterface
) {
throw new \InvalidArgumentException('metadataFactory must be a instanceof MetadataFactoryInterface');
}
$this->metadataFactory = $metadataFactory;
}
public function find(FormInterface $form)
{
$propertyPath = $form->getPropertyPath();
if ($form->getPropertyPath() === null) {
return new ConstraintCollection();
}
$class = $this->resolveDataClass($form);
if ($class === null) {
return new ConstraintCollection();
}
$metadata = $this->metadataFactory->getMetadataFor($class);
if (!$metadata instanceof ClassMetadata) {
return new ConstraintCollection();
}
if ($propertyPath->getLength() < 1) {
throw new UnsupportedException('Not supported please submit a issue with the form that produces this error!');
}
// Retrieve the last property element
$propertyLastElementIndex = $propertyPath->getLength() - 1;
$propertyName = $propertyPath->getElement($propertyLastElementIndex);
if ($propertyPath->getLength() > 1) {
// When we have multiple parts to the path then resolve it
// To return the actual property and metadata
// Resolve parent data
list($dataSource, $dataSourceClass) = $this->resolveDataSource($form);
for ($i = 0; $i < $propertyPath->getLength() - 1; $i++) {
$element = $propertyPath->getElement($i);
$property = $this->guessProperty($metadata, $element);
// If the Valid tag is missing the property will return null.
// Or if there is no data set on the form
if ($property === null) {
return new ConstraintCollection();
}
foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
if (!$propertyMetadata instanceof MemberMetadata) {
continue;
}
$dataSourceInfo = $this->findPropertyDataTypeInfo($propertyMetadata, $dataSource, $dataSourceClass);
if ($dataSourceInfo === null) {
return new ConstraintCollection();
}
list($dataSourceClass, $dataSource) = $dataSourceInfo;
// Handle arrays/index based properties
while ($dataSourceClass === null) {
$i++;
if (!$propertyPath->isIndex($i)) {
// For some strange reason the findPropertyDataTypeInfo is wrong
// or the form is wrong
return new ConstraintCollection();
}
$dataSource = $dataSource[$propertyPath->getElement($i)];
if (is_object($dataSource)) {
$dataSourceClass = get_class($dataSource);
}
}
// Ok we failed to find the data source class
if ($dataSourceClass === null) {
return new ConstraintCollection();
}
$metadata = $this->metadataFactory->getMetadataFor($dataSourceClass);
if (!$metadata instanceof ClassMetadata) {
continue;
}
continue 2;
}
// We where unable to locate a class/array property
return new ConstraintCollection();
}
}
// Handle array properties
$propertyCascadeOnly = false;
if ($propertyPath->isIndex($propertyLastElementIndex)) {
$propertyCascadeOnly = true;
$elements = $form->getParent()->getPropertyPath()->getElements();
$propertyName = end($elements);
}
// Find property constraints
return $this->findPropertyConstraints($metadata, $propertyName, $propertyCascadeOnly);
}
private function findPropertyConstraints(ClassMetadata $metadata, $propertyName, $cascadingOnly = false)
{
$constraintCollection = new ConstraintCollection();
$property = $this->guessProperty($metadata, $propertyName);
if ($property === null) {
return $constraintCollection;
}
foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
if (!$propertyMetadata instanceof MemberMetadata) {
continue;
}
// For some reason Valid constraint is not in the list of constraints so we hack it in ....
$this->addCascadingValidConstraint($propertyMetadata, $constraintCollection);
if ($cascadingOnly) {
continue;
}
// Add the actual constraints
$constraintCollection->addCollection(
new ConstraintCollection($propertyMetadata->getConstraints())
);
}
return $constraintCollection;
}
/**
* Gets the form root data class used by the given form.
*
* @param FormInterface $form
* @return string|null
*/
private function resolveDataClass(FormInterface $form)
{
// Nothing to do if root
if ($form->isRoot()) {
return $form->getConfig()->getDataClass();
}
$propertyPath = $form->getPropertyPath();
/** @var FormInterface $dataForm */
$dataForm = $form;
// If we have a index then we need to use it's parent
if ($propertyPath->getLength() === 1 && $propertyPath->isIndex(0) && $form->getConfig()->getCompound()) {
return $this->resolveDataClass($form->getParent());
}
// Now locate the closest data class
// TODO what is the length really for?
for ($i = $propertyPath->getLength(); $i !== 0; $i--) {
$dataForm = $dataForm->getParent();
# When a data class is found then use that form
# This happend when property_path contains multiple parts aka `entity.prop`
if ($dataForm->getConfig()->getDataClass() !== null) {
break;
}
}
// If the root inherits data, then grab the parent
if ($dataForm->getConfig()->getInheritData()) {
$dataForm = $dataForm->getParent();
}
return $dataForm->getConfig()->getDataClass();
}
/**
* Gets the form data to which a property path applies
*
* @param FormInterface $form
* @return object|null
*/
private function resolveDataSource(FormInterface $form)
{
if ($form->isRoot()) {
// Nothing to do if root
$dataForm = $form->getData();
} else {
$dataForm = $form;
while ($dataForm->getConfig()->getDataClass() === null) {
$dataForm = $form->getParent();
}
}
$data = $dataForm->getData();
return array(
$data,
$data === null ? $dataForm->getConfig()->getDataClass() : get_class($data)
);
}
private function addCascadingValidConstraint(MemberMetadata $propertyMetadata, ConstraintCollection $constraintCollection)
{
if (method_exists($propertyMetadata, 'getCascadingStrategy')) {
if ($propertyMetadata->getCascadingStrategy() === CascadingStrategy::CASCADE) {
$constraintCollection->add(new Valid());
}
} else {
if ($propertyMetadata->isCollectionCascaded()) {
$constraintCollection->add(new Valid());
}
}
}
/**
* Returns the lowerCamelCase form of a string.
*
* @param string $string The string to camelize.
* @return string The camelized version of the string
*/
private function camelize($string)
{
return lcfirst(strtr(ucwords(strtr($string, array('_' => ' '))), array(' ' => '')));
}
/**
* Guess what property a given element belongs to.
*
* @param ClassMetadata $metadata
* @param string $element
* @return null|string
*/
private function guessProperty(ClassMetadata $metadata, $element)
{
// Is it the element the actual property
if ($metadata->hasPropertyMetadata($element)) {
return $element;
}
// Is it a camelized property
$camelized = $this->camelize($element);
if ($metadata->hasPropertyMetadata($camelized)) {
return $camelized;
}
return null;
}
/**
* @param MemberMetadata $propertyMetadata
* @param mixed $dataSource
* @param string $dataSourceClass
* @return null|array
*/
protected function findPropertyDataTypeInfo(MemberMetadata $propertyMetadata, $dataSource, $dataSourceClass)
{
if ($dataSource !== null) {
$dataSource = $propertyMetadata
->getReflectionMember($dataSourceClass)
->getValue($dataSource);
if (is_array($dataSource) || $dataSource instanceof \ArrayAccess) {
return array(null, $dataSource);
}
if (is_object($dataSource)) {
return array(get_class($dataSource), $dataSource);
}
return null;
}
// Since there is no datasource we need another way to determin the properties class
foreach ($propertyMetadata->getConstraints() as $constraint) {
if (!$constraint instanceof Type) {
continue;
}
$type = strtolower($constraint->type);
$type = $type === 'boolean' ? 'bool' : $constraint->type;
$isFunction = 'is_' . $type;
$ctypeFunction = 'ctype_' . $type;
if (function_exists($isFunction) || function_exists($ctypeFunction)) {
return null;
}
return array($constraint->type, null);
}
return null;
}
}