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