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