1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Processor; |
4
|
|
|
|
5
|
|
|
use Bdf\Form\Aggregate\FormBuilderInterface; |
6
|
|
|
use Bdf\Form\Attribute\AttributeForm; |
7
|
|
|
use Bdf\Form\Attribute\Button\ButtonBuilderAttributeInterface; |
8
|
|
|
use Bdf\Form\Attribute\ChildBuilderAttributeInterface; |
9
|
|
|
use Bdf\Form\Attribute\Form\FormBuilderAttributeInterface; |
10
|
|
|
use Bdf\Form\Attribute\Processor\Element\ConstraintAttributeProcessor; |
11
|
|
|
use Bdf\Form\Attribute\Processor\Element\ElementAttributeProcessorInterface; |
12
|
|
|
use Bdf\Form\Attribute\Processor\Element\ExtractorAttributeProcessor; |
13
|
|
|
use Bdf\Form\Attribute\Processor\Element\FilterAttributeProcessor; |
14
|
|
|
use Bdf\Form\Attribute\Processor\Element\HydratorAttributeProcessor; |
15
|
|
|
use Bdf\Form\Attribute\Processor\Element\TransformerAttributeProcessor; |
16
|
|
|
use ReflectionAttribute; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use ReflectionProperty; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Strategy for directly configure the form builder using attributes |
22
|
|
|
*/ |
23
|
|
|
final class ConfigureFormBuilderStrategy implements ReflectionStrategyInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var list<ElementAttributeProcessorInterface> |
27
|
|
|
*/ |
28
|
|
|
private array $elementProcessors = []; |
29
|
|
|
|
30
|
53 |
|
public function __construct() |
31
|
|
|
{ |
32
|
53 |
|
$this->registerElementAttributeProcessor(new ConstraintAttributeProcessor()); |
33
|
53 |
|
$this->registerElementAttributeProcessor(new FilterAttributeProcessor()); |
34
|
53 |
|
$this->registerElementAttributeProcessor(new TransformerAttributeProcessor()); |
35
|
53 |
|
$this->registerElementAttributeProcessor(new HydratorAttributeProcessor()); |
36
|
53 |
|
$this->registerElementAttributeProcessor(new ExtractorAttributeProcessor()); |
37
|
53 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
52 |
|
public function onFormClass(ReflectionClass $formClass, AttributeForm $form, FormBuilderInterface $builder): void |
43
|
|
|
{ |
44
|
52 |
|
foreach ($formClass->getAttributes(FormBuilderAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
45
|
14 |
|
$attribute->newInstance()->applyOnFormBuilder($form, $builder); |
46
|
|
|
} |
47
|
52 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
3 |
|
public function onButtonProperty(ReflectionProperty $property, string $name, AttributeForm $form, FormBuilderInterface $builder): void |
53
|
|
|
{ |
54
|
3 |
|
$submitBuilder = $builder->submit($name); |
55
|
|
|
|
56
|
3 |
|
foreach ($property->getAttributes(ButtonBuilderAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
57
|
2 |
|
$attribute->newInstance()->applyOnButtonBuilder($form, $submitBuilder); |
58
|
|
|
} |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
44 |
|
public function onElementProperty(ReflectionProperty $property, string $name, string $elementType, AttributeForm $form, FormBuilderInterface $builder): void |
65
|
|
|
{ |
66
|
44 |
|
$elementBuilder = $builder->add($name, $elementType); |
67
|
|
|
|
68
|
44 |
|
foreach ($property->getAttributes() as $attribute) { |
69
|
44 |
|
$attributeInstance = $attribute->newInstance(); |
70
|
|
|
|
71
|
44 |
|
if ($attributeInstance instanceof ChildBuilderAttributeInterface) { |
72
|
|
|
/** @var ChildBuilderAttributeInterface $attributeInstance */ |
73
|
28 |
|
$attributeInstance->applyOnChildBuilder($form, $elementBuilder); |
74
|
28 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
23 |
|
foreach ($this->elementProcessors as $configurator) { |
78
|
23 |
|
if ($attributeInstance instanceof ($configurator->type())) { |
|
|
|
|
79
|
23 |
|
$configurator->process($elementBuilder, $attributeInstance); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
44 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
52 |
|
public function onPostConfigure(array $elementProperties, array $buttonProperties, AttributeForm $form): ?PostConfigureInterface |
89
|
|
|
{ |
90
|
52 |
|
return new PostConfigureReflectionSetProperties($elementProperties, $buttonProperties); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Register a new processor for element attributes |
95
|
|
|
* |
96
|
|
|
* @param ElementAttributeProcessorInterface<T> $processor |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
* |
100
|
|
|
* @template T as object |
101
|
|
|
*/ |
102
|
53 |
|
private function registerElementAttributeProcessor(ElementAttributeProcessorInterface $processor): void |
103
|
|
|
{ |
104
|
53 |
|
$this->elementProcessors[] = $processor; |
105
|
53 |
|
} |
106
|
|
|
} |
107
|
|
|
|