DefaultValue   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 25
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCodeForChildBuilder() 0 3 1
A __construct() 0 6 1
A applyOnChildBuilder() 0 3 1
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