ModelTransformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 36
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCodeForChildBuilder() 0 4 1
A __construct() 0 16 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
use Bdf\Form\Transformer\TransformerInterface;
12
use Nette\PhpGenerator\Literal;
13
14
/**
15
 * Add a model transformer on the child, using a transformer class
16
 *
17
 * This attribute is equivalent to call :
18
 * <code>
19
 * $builder->string('foo')->modelTransformer(new MyTransformer(...$arguments));
20
 * </code>
21
 *
22
 * Usage:
23
 * <code>
24
 * class MyForm extends AttributeForm
25
 * {
26
 *     #[ModelTransformer(MyTransformer::class, ['foo', 'bar'])]
27
 *     private IntegerElement $foo;
28
 * }
29
 * </code>
30
 *
31
 * @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface>
32
 *
33
 * @see ChildBuilderInterface::modelTransformer() The called method
34
 * @see CallbackModelTransformer For use custom methods as transformer instead of class
35
 */
36
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
37
final class ModelTransformer implements ChildBuilderAttributeInterface
38
{
39 3
    public function __construct(
40
        /**
41
         * The transformer class name
42
         *
43
         * @var class-string<TransformerInterface>
44
         * @readonly
45
         */
46
        private string $transformerClass,
47
        /**
48
         * Arguments to provide on the transformer constructor
49
         *
50
         * @var array
51
         * @readonly
52
         */
53
        private array $constructorArguments = [],
54
    ) {
55 3
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
61
    {
62 2
        $builder->modelTransformer(new $this->transformerClass(...$this->constructorArguments));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
69
    {
70 1
        $transformer = $generator->useAndSimplifyType($this->transformerClass);
71 1
        $generator->line('$?->modelTransformer(new ?(...?));', [$name, new Literal($transformer), $this->constructorArguments]);
72
    }
73
}
74