/
FormConfig.php
635 lines (546 loc) · 13.8 KB
/
FormConfig.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* A basic form configuration.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormConfig implements FormConfigInterface
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var string
*/
private $name;
/**
* @var PropertyPath
*/
private $propertyPath;
/**
* @var Boolean
*/
private $mapped;
/**
* @var Boolean
*/
private $virtual;
/**
* @var array
*/
private $types = array();
/**
* @var array
*/
private $clientTransformers = array();
/**
* @var array
*/
private $normTransformers = array();
/**
* @var DataMapperInterface
*/
private $dataMapper;
/**
* @var array
*/
private $validators = array();
/**
* @var Boolean
*/
private $required;
/**
* @var Boolean
*/
private $disabled;
/**
* @var Boolean
*/
private $errorBubbling;
/**
* @var mixed
*/
private $emptyData;
/**
* @var array
*/
private $attributes = array();
/**
* @var mixed
*/
private $data;
/**
* @var string
*/
private $dataClass;
/**
* Creates an empty form configuration.
*
* @param string $name The form name.
* @param string $dataClass The class of the form's data.
* @param EventDispatcherInterface $dispatcher The event dispatcher.
*
* @throws UnexpectedTypeException If the name is not a string.
* @throws \InvalidArgumentException If the data class is not a valid class or if
* the name contains invalid characters.
*/
public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher)
{
$name = (string) $name;
self::validateName($name);
if (null !== $dataClass && !class_exists($dataClass)) {
throw new \InvalidArgumentException(sprintf('The data class "%s" is not a valid class.', $dataClass));
}
$this->name = $name;
$this->dataClass = $dataClass;
$this->dispatcher = $dispatcher;
}
/**
* Adds an event listener to an event on this form.
*
* @param string $eventName The name of the event to listen to.
* @param callable $listener The listener to execute.
* @param integer $priority The priority of the listener. Listeners
* with a higher priority are called before
* listeners with a lower priority.
*
* @return self The configuration object.
*/
public function addEventListener($eventName, $listener, $priority = 0)
{
$this->dispatcher->addListener($eventName, $listener, $priority);
return $this;
}
/**
* Adds an event subscriber for events on this form.
*
* @param EventSubscriberInterface $subscriber The subscriber to attach.
*
* @return self The configuration object.
*/
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
$this->dispatcher->addSubscriber($subscriber);
return $this;
}
/**
* Adds a validator to the form.
*
* @param FormValidatorInterface $validator The validator.
*
* @return self The configuration object.
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
public function addValidator(FormValidatorInterface $validator)
{
$this->validators[] = $validator;
return $this;
}
/**
* Appends a transformer to the client transformer chain
*
* @param DataTransformerInterface $clientTransformer
*
* @return self The configuration object.
*/
public function appendClientTransformer(DataTransformerInterface $clientTransformer)
{
$this->clientTransformers[] = $clientTransformer;
return $this;
}
/**
* Prepends a transformer to the client transformer chain.
*
* @param DataTransformerInterface $clientTransformer
*
* @return self The configuration object.
*/
public function prependClientTransformer(DataTransformerInterface $clientTransformer)
{
array_unshift($this->clientTransformers, $clientTransformer);
return $this;
}
/**
* Clears the client transformers.
*
* @return self The configuration object.
*/
public function resetClientTransformers()
{
$this->clientTransformers = array();
return $this;
}
/**
* Appends a transformer to the normalization transformer chain
*
* @param DataTransformerInterface $normTransformer
*
* @return self The configuration object.
*/
public function appendNormTransformer(DataTransformerInterface $normTransformer)
{
$this->normTransformers[] = $normTransformer;
return $this;
}
/**
* Prepends a transformer to the normalization transformer chain
*
* @param DataTransformerInterface $normTransformer
*
* @return self The configuration object.
*/
public function prependNormTransformer(DataTransformerInterface $normTransformer)
{
array_unshift($this->normTransformers, $normTransformer);
return $this;
}
/**
* Clears the normalization transformers.
*
* @return self The configuration object.
*/
public function resetNormTransformers()
{
$this->normTransformers = array();
return $this;
}
/**
* {@inheritdoc}
*/
public function getEventDispatcher()
{
return $this->dispatcher;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getPropertyPath()
{
return $this->propertyPath;
}
/**
* {@inheritdoc}
*/
public function getMapped()
{
return $this->mapped;
}
/**
* {@inheritdoc}
*/
public function getVirtual()
{
return $this->virtual;
}
/**
* {@inheritdoc}
*/
public function getTypes()
{
return $this->types;
}
/**
* {@inheritdoc}
*/
public function getClientTransformers()
{
return $this->clientTransformers;
}
/**
* {@inheritdoc}
*/
public function getNormTransformers()
{
return $this->normTransformers;
}
/**
* Returns the data mapper of the form.
*
* @return DataMapperInterface The data mapper.
*/
public function getDataMapper()
{
return $this->dataMapper;
}
/**
* {@inheritdoc}
*/
public function getValidators()
{
return $this->validators;
}
/**
* {@inheritdoc}
*/
public function getRequired()
{
return $this->required;
}
/**
* {@inheritdoc}
*/
public function getDisabled()
{
return $this->disabled;
}
/**
* {@inheritdoc}
*/
public function getErrorBubbling()
{
return $this->errorBubbling;
}
/**
* {@inheritdoc}
*/
public function getEmptyData()
{
return $this->emptyData;
}
/**
* {@inheritdoc}
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* {@inheritdoc}
*/
public function hasAttribute($name)
{
return isset($this->attributes[$name]);
}
/**
* {@inheritdoc}
*/
public function getAttribute($name)
{
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
}
/**
* {@inheritdoc}
*/
public function getData()
{
return $this->data;
}
/**
* {@inheritdoc}
*/
public function getDataClass()
{
return $this->dataClass;
}
/**
* Sets the value for an attribute.
*
* @param string $name The name of the attribute
* @param string $value The value of the attribute
*
* @return self The configuration object.
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
* Sets the attributes.
*
* @param array $attributes The attributes.
*
* @return self The configuration object.
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
* Sets the data mapper used by the form.
*
* @param DataMapperInterface $dataMapper
*
* @return self The configuration object.
*/
public function setDataMapper(DataMapperInterface $dataMapper = null)
{
$this->dataMapper = $dataMapper;
return $this;
}
/**
* Set whether the form is disabled.
*
* @param Boolean $disabled Whether the form is disabled
*
* @return self The configuration object.
*/
public function setDisabled($disabled)
{
$this->disabled = (Boolean) $disabled;
return $this;
}
/**
* Sets the data used for the client data when no value is bound.
*
* @param mixed $emptyData The empty data.
*
* @return self The configuration object.
*/
public function setEmptyData($emptyData)
{
$this->emptyData = $emptyData;
return $this;
}
/**
* Sets whether errors bubble up to the parent.
*
* @param Boolean $errorBubbling
*
* @return self The configuration object.
*/
public function setErrorBubbling($errorBubbling)
{
$this->errorBubbling = null === $errorBubbling ? null : (Boolean) $errorBubbling;
return $this;
}
/**
* Sets whether this field is required to be filled out when bound.
*
* @param Boolean $required
*
* @return self The configuration object.
*/
public function setRequired($required)
{
$this->required = (Boolean) $required;
return $this;
}
/**
* Sets the property path that the form should be mapped to.
*
* @param string|PropertyPath $propertyPath The property path or null if the path
* should be set automatically based on
* the form's name.
*
* @return self The configuration object.
*/
public function setPropertyPath($propertyPath)
{
if (null !== $propertyPath && !$propertyPath instanceof PropertyPath) {
$propertyPath = new PropertyPath($propertyPath);
}
$this->propertyPath = $propertyPath;
return $this;
}
/**
* Sets whether the form should be mapped to an element of its
* parent's data.
*
* @param Boolean $mapped Whether the form should be mapped.
*
* @return self The configuration object.
*/
public function setMapped($mapped)
{
$this->mapped = $mapped;
return $this;
}
/**
* Sets whether the form should be virtual.
*
* @param Boolean $virtual Whether the form should be virtual.
*
* @return self The configuration object.
*/
public function setVirtual($virtual)
{
$this->virtual = $virtual;
return $this;
}
/**
* Set the types.
*
* @param array $types An array FormTypeInterface
*
* @return self The configuration object.
*/
public function setTypes(array $types)
{
$this->types = $types;
return $this;
}
/**
* Sets the initial data of the form.
*
* @param array $data The data of the form in application format.
*
* @return self The configuration object.
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Validates whether the given variable is a valid form name.
*
* @param string $name The tested form name.
*
* @throws UnexpectedTypeException If the name is not a string.
* @throws \InvalidArgumentException If the name contains invalid characters.
*/
static public function validateName($name)
{
if (!is_string($name)) {
throw new UnexpectedTypeException($name, 'string');
}
if (!self::isValidName($name)) {
throw new \InvalidArgumentException(sprintf(
'The name "%s" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").',
$name
));
}
}
/**
* Returns whether the given variable contains a valid form name.
*
* A name is accepted if it
*
* * is empty
* * starts with a letter, digit or underscore
* * contains only letters, digits, numbers, underscores ("_"),
* hyphens ("-") and colons (":")
*
* @param string $name The tested form name.
*
* @return Boolean Whether the name is valid.
*/
static public function isValidName($name)
{
return '' === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
}
}