DefaultValue::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 0
Metric Value
cc 1
eloc 1
c 0
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\Child;
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\Attribute\Processor\GenerateConfiguratorStrategy;
10
use Bdf\Form\Child\ChildBuilderInterface;
11
12
/**
13
 * Define the input default value
14
 * The value must be the PHP value (i.e. the parsed HTTP value)
15
 *
16
 * Note: this attribute cannot be repeated
17
 *
18
 * This attribute is equivalent to call :
19
 * <code>
20
 * $builder->float('foo')->default(12.3);
21
 * </code>
22
 *
23
 * Usage:
24
 * <code>
25
 * class MyForm extends AttributeForm
26
 * {
27
 *     #[DefaultValue(12.3)]
28
 *     private FloatElement $foo;
29
 * }
30
 * </code>
31
 *
32
 * @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface>
33
 *
34
 * @see ChildBuilderInterface::default() The called method
35
 */
36
#[Attribute(Attribute::TARGET_PROPERTY)]
37
final class DefaultValue implements ChildBuilderAttributeInterface
38
{
39 3
    public function __construct(
40
        /**
41
         * @readonly
42
         */
43
        private mixed $default
44
    ) {
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
51
    {
52 1
        $builder->default($this->default);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2
    public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
59
    {
60 2
        $generator->line('$?->default(?);', [$name, $this->default]);
61
    }
62
}
63