Repository navigation
Expand file tree
/
Copy pathDataClassType.php
More file actions
92 lines (80 loc) · 2.86 KB
/
Copy pathDataClassType.php
File metadata and controls
92 lines (80 loc) · 2.86 KB
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
<?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\Attribute\AsFormType;
use Symfony\Component\Form\Attribute\FormField;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* A form type derived at compile time from a class marked with the AsFormType attribute.
*
* @author Benjamin Georgeault <git@wedgesama.fr>
*
* @internal
*/
final class DataClassType extends AbstractType
{
/**
* @param class-string $dataClass
* @param class-string $parent
* @param array<string, array{class-string|null, array<string, mixed>}> $fields
* @param array<string, mixed> $options
*/
public function __construct(
private string $dataClass,
private string $parent,
private string $blockPrefix,
private array $fields,
private array $options,
) {
}
public function getParent(): string
{
return $this->parent;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults($this->options + ['data_class' => $this->dataClass]);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
foreach ($this->fields as $name => [$type, $fieldOptions]) {
$builder->add($name, $type, $fieldOptions);
}
}
public function getBlockPrefix(): string
{
return $this->blockPrefix;
}
/**
* @return class-string
*/
public function getDataClass(): string
{
return $this->dataClass;
}
/**
* Reads a closure from the options of the attribute declaring it, as closures cannot be dumped in the container.
*
* @param class-string $class The class declaring the attribute
* @param string|null $property The property declaring the #[FormField] attribute, null for the #[AsFormType] attribute
* @param list<array-key> $keys The keys leading to the closure in the options of the attribute
*/
public static function getOptionClosure(string $class, ?string $property, array $keys): \Closure
{
$attributes = null === $property
? (new \ReflectionClass($class))->getAttributes(AsFormType::class, \ReflectionAttribute::IS_INSTANCEOF)
: (new \ReflectionProperty($class, $property))->getAttributes(FormField::class, \ReflectionAttribute::IS_INSTANCEOF);
$value = $attributes[0]->newInstance()->options;
foreach ($keys as $key) {
$value = $value[$key];
}
return $value;
}
}