DateTimeClass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 9
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
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