DateFormat::generateCodeForChildBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
11
/**
12
 * Define the date format to use for parse the date string
13
 *
14
 * Note: this attribute is not repeatable
15
 *
16
 * This attribute is equivalent to call :
17
 * <code>
18
 * $builder->dateTime('date')->format('d/m/Y H:i');
19
 * </code>
20
 *
21
 * Usage:
22
 * <code>
23
 * class MyForm extends AttributeForm
24
 * {
25
 *     #[DateFormat('d/m/Y H:i')]
26
 *     private DateTimeElement $foo;
27
 * }
28
 * </code>
29
 *
30
 * @see \Bdf\Form\Leaf\Date\DateTimeElementBuilder::format() The called method
31
 *
32
 * @implements ChildBuilderAttributeInterface<\Bdf\Form\Leaf\Date\DateTimeElementBuilder>
33
 */
34
#[Attribute(Attribute::TARGET_PROPERTY)]
35
final class DateFormat implements ChildBuilderAttributeInterface
36
{
37 3
    public function __construct(
38
        /**
39
         * The date format
40
         *
41
         * @var non-empty-string
42
         * @readonly
43
         *
44
         * @see https://www.php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters For the format
45
         */
46
        private string $format,
47
    ) {
48 3
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
54
    {
55 1
        $builder->format($this->format);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
62
    {
63 2
        $generator->line('$?->format(?);', [$name, $this->format]);
64
    }
65
}
66