|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Attribute; |
|
6
|
|
|
use Bdf\Form\Aggregate\FormBuilderInterface; |
|
7
|
|
|
use Bdf\Form\Aggregate\Value\ValueGenerator; |
|
8
|
|
|
use Bdf\Form\Attribute\AttributeForm; |
|
9
|
|
|
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator; |
|
10
|
|
|
use Bdf\Form\Attribute\Processor\GenerateConfiguratorStrategy; |
|
11
|
|
|
use Nette\PhpGenerator\Method; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Define the value generator of the form, using a callback method |
|
15
|
|
|
* |
|
16
|
|
|
* Note: this attribute is not repeatable |
|
17
|
|
|
* |
|
18
|
|
|
* This attribute is equivalent to call : |
|
19
|
|
|
* <code> |
|
20
|
|
|
* $builder->generates([$this, 'generateValue']); |
|
21
|
|
|
* </code> |
|
22
|
|
|
* |
|
23
|
|
|
* Usage: |
|
24
|
|
|
* <code> |
|
25
|
|
|
* #[CallbackGenerator('generateValue')] |
|
26
|
|
|
* class MyForm extends AttributeForm |
|
27
|
|
|
* { |
|
28
|
|
|
* public function generateValue(FormInterface $form) |
|
29
|
|
|
* { |
|
30
|
|
|
* return new Foo(); |
|
31
|
|
|
* } |
|
32
|
|
|
* } |
|
33
|
|
|
* </code> |
|
34
|
|
|
* |
|
35
|
|
|
* @see FormBuilderInterface::generates() The called method |
|
36
|
|
|
* @see ValueGenerator |
|
37
|
|
|
* @see Generates For generate with a simple class name |
|
38
|
|
|
*/ |
|
39
|
|
|
#[Attribute(Attribute::TARGET_CLASS)] |
|
40
|
|
|
final class CallbackGenerator implements FormBuilderAttributeInterface |
|
41
|
|
|
{ |
|
42
|
3 |
|
public function __construct( |
|
43
|
|
|
/** |
|
44
|
|
|
* The method name use for generate the form value |
|
45
|
|
|
* This method should be public, and declared on the form class, following the prototype : |
|
46
|
|
|
* `public function (FormInterface $form): mixed` |
|
47
|
|
|
* |
|
48
|
|
|
* @readonly |
|
49
|
|
|
*/ |
|
50
|
|
|
private string $callback, |
|
51
|
|
|
) { |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function applyOnFormBuilder(AttributeForm $form, FormBuilderInterface $builder): void |
|
58
|
|
|
{ |
|
59
|
2 |
|
$builder->generates([$form, $this->callback]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function generateCodeForFormBuilder(AttributesProcessorGenerator $generator, AttributeForm $form): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
$generator->line('$builder->generates([$this, ?]);', [$this->callback]); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|