Configure::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 a custom configuration method for an element
14
 *
15
 * Use this attribute when other attributes cannot be used to configure the current element
16
 * Takes as argument the method name to use. This method must be declared into the form class with public visibility
17
 *
18
 * Usage:
19
 * <code>
20
 * class MyForm extends AttributeForm
21
 * {
22
 *     #[Configure('configureFoo')]
23
 *     private IntegerElement $foo;
24
 *
25
 *     public function configureFoo(ChildBuilderInterface $builder): void
26
 *     {
27
 *         $builder->min(5);
28
 *     }
29
 * }
30
 * </code>
31
 *
32
 * @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface>
33
 */
34
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
35
class Configure implements ChildBuilderAttributeInterface
36
{
37 3
    public function __construct(
38
        /**
39
         * The method name to use as configurator
40
         * The method should follow the prototype `public function (ChildBuilderInterface $builder): void`
41
         *
42
         * @var literal-string
43
         * @readonly
44
         */
45
        private string $callback
46
    ) {
47 3
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
53
    {
54 1
        $form->{$this->callback}($builder);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
61
    {
62 2
        $generator->line('$form->?($?);', [$this->callback, $name]);
63
    }
64
}
65