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
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Define a custom configuration method for an element |
14
|
|
|
* |
15
|
|
|
* Use this attribute when other attributes cannot be used to configure the current element |
16
|
|
|
* Takes as argument the method name to use. This method must be declared into the form class with public visibility |
17
|
|
|
* |
18
|
|
|
* Usage: |
19
|
|
|
* <code> |
20
|
|
|
* class MyForm extends AttributeForm |
21
|
|
|
* { |
22
|
|
|
* #[Configure('configureFoo')] |
23
|
|
|
* private IntegerElement $foo; |
24
|
|
|
* |
25
|
|
|
* public function configureFoo(ChildBuilderInterface $builder): void |
26
|
|
|
* { |
27
|
|
|
* $builder->min(5); |
28
|
|
|
* } |
29
|
|
|
* } |
30
|
|
|
* </code> |
31
|
|
|
* |
32
|
|
|
* @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface> |
33
|
|
|
*/ |
34
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
35
|
|
|
class Configure implements ChildBuilderAttributeInterface |
36
|
|
|
{ |
37
|
3 |
|
public function __construct( |
38
|
|
|
/** |
39
|
|
|
* The method name to use as configurator |
40
|
|
|
* The method should follow the prototype `public function (ChildBuilderInterface $builder): void` |
41
|
|
|
* |
42
|
|
|
* @var literal-string |
43
|
|
|
* @readonly |
44
|
|
|
*/ |
45
|
|
|
private string $callback |
46
|
|
|
) { |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void |
53
|
|
|
{ |
54
|
1 |
|
$form->{$this->callback}($builder); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void |
61
|
|
|
{ |
62
|
2 |
|
$generator->line('$form->?($?);', [$this->callback, $name]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|