1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Child; |
4
|
|
|
|
5
|
|
|
use Attribute; |
6
|
|
|
use Bdf\Form\Attribute\AttributeForm; |
7
|
|
|
use Bdf\Form\Attribute\ChildBuilderAttributeInterface; |
8
|
|
|
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator; |
9
|
|
|
use Bdf\Form\Attribute\Processor\GenerateConfiguratorStrategy; |
10
|
|
|
use Bdf\Form\Child\ChildBuilderInterface; |
11
|
|
|
use Bdf\Form\PropertyAccess\Getter; |
12
|
|
|
use Bdf\Form\PropertyAccess\Setter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Define simple hydrator and extractor on the element, using the name property or accessor name |
16
|
|
|
* |
17
|
|
|
* This attribute is equivalent to call : |
18
|
|
|
* <code> |
19
|
|
|
* $builder->float('foo')->getter('bar')->setter('bar'); |
20
|
|
|
* $builder->float('foo')->hydrator(new Setter('bar'))->extract(new Getter('bar')); |
21
|
|
|
* </code> |
22
|
|
|
* |
23
|
|
|
* Usage: |
24
|
|
|
* <code> |
25
|
|
|
* class MyForm extends AttributeForm |
26
|
|
|
* { |
27
|
|
|
* #[GetSet('bar')] |
28
|
|
|
* private FloatElement $foo; |
29
|
|
|
* } |
30
|
|
|
* </code> |
31
|
|
|
* |
32
|
|
|
* @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface> |
33
|
|
|
* |
34
|
|
|
* @see ChildBuilder::getter() |
35
|
|
|
* @see ChildBuilder::setter() |
36
|
|
|
* @see ChildBuilderInterface::hydrator() |
37
|
|
|
* @see ChildBuilderInterface::extractor() |
38
|
|
|
* |
39
|
|
|
* @see Getter For define only the extractor |
40
|
|
|
* @see Setter For define only the hydrator |
41
|
|
|
*/ |
42
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
43
|
|
|
final class GetSet implements ChildBuilderAttributeInterface |
44
|
|
|
{ |
45
|
10 |
|
public function __construct( |
46
|
|
|
/** |
47
|
|
|
* The property name to use |
48
|
|
|
* This can be a public property, or public accessor method |
49
|
|
|
* (optionally starting with get for the getter, and starting with set for the setter) |
50
|
|
|
* |
51
|
|
|
* If not provided, the input name will be used as property name |
52
|
|
|
* |
53
|
|
|
* @readonly |
54
|
|
|
*/ |
55
|
|
|
private ?string $propertyName = null, |
56
|
|
|
) { |
57
|
10 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
4 |
|
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void |
63
|
|
|
{ |
64
|
4 |
|
$builder->hydrator(new Setter($this->propertyName))->extractor(new Getter($this->propertyName)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
6 |
|
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void |
71
|
|
|
{ |
72
|
6 |
|
$generator->use(Setter::class)->use(Getter::class); |
73
|
6 |
|
$generator->line('$?->hydrator(new Setter(?))->extractor(new Getter(?));', [$name, $this->propertyName, $this->propertyName]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|