1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Element\Date; |
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\Child\ChildBuilderInterface; |
10
|
|
|
use Nette\PhpGenerator\Literal; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Define the DateTime type to use on the date input |
14
|
|
|
* |
15
|
|
|
* Note: this attribute is not repeatable |
16
|
|
|
* |
17
|
|
|
* This attribute is equivalent to call : |
18
|
|
|
* <code> |
19
|
|
|
* $builder->dateTime('date')->className(Carbon::class); |
20
|
|
|
* </code> |
21
|
|
|
* |
22
|
|
|
* Usage: |
23
|
|
|
* <code> |
24
|
|
|
* class MyForm extends AttributeForm |
25
|
|
|
* { |
26
|
|
|
* #[DateTimeClass(Carbon::class)] |
27
|
|
|
* private DateTimeElement $foo; |
28
|
|
|
* } |
29
|
|
|
* </code> |
30
|
|
|
* |
31
|
|
|
* @see \Bdf\Form\Leaf\Date\DateTimeElementBuilder::className() The called method |
32
|
|
|
* |
33
|
|
|
* @implements ChildBuilderAttributeInterface<\Bdf\Form\Leaf\Date\DateTimeElementBuilder> |
34
|
|
|
*/ |
35
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
36
|
|
|
final class DateTimeClass implements ChildBuilderAttributeInterface |
37
|
|
|
{ |
38
|
3 |
|
public function __construct( |
39
|
|
|
/** |
40
|
|
|
* The datetime class to use |
41
|
|
|
* |
42
|
|
|
* @var class-string<\DateTimeInterface> |
43
|
|
|
* @readonly |
44
|
|
|
*/ |
45
|
|
|
private string $className, |
46
|
|
|
) { |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void |
53
|
|
|
{ |
54
|
1 |
|
$builder->className($this->className); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void |
61
|
|
|
{ |
62
|
2 |
|
$generator->line('$?->className(?::class);', [$name, new Literal($generator->useAndSimplifyType($this->className))]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|