Count::applyOnChildBuilder()   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 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bdf\Form\Attribute\Aggregate;
4
5
use Attribute;
6
use Bdf\Form\Aggregate\ArrayElementBuilder;
7
use Bdf\Form\Attribute\AttributeForm;
8
use Bdf\Form\Attribute\ChildBuilderAttributeInterface;
9
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator;
10
use Bdf\Form\Child\ChildBuilderInterface;
11
use Symfony\Component\Validator\Constraints\Count as CountConstraint;
12
13
/**
14
 * Add a Count constraint on the array element
15
 *
16
 * This attribute is equivalent to call one of those :
17
 * <code>
18
 * $builder->array('values')->count(['min' => 3]);
19
 * $builder->array('values')->arrayConstraint(new Count(min: 3));
20
 * </code>
21
 *
22
 * Usage:
23
 * <code>
24
 * class MyForm extends AttributeForm
25
 * {
26
 *     #[Count(min: 3, max: 42)]
27
 *     private ArrayElement $values;
28
 * }
29
 * </code>
30
 *
31
 * @see CountConstraint The used constraint
32
 * @see ArrayElementBuilder::arrayConstraint() The called method
33
 * @see ArrayElementBuilder::count() Equivalent method call
34
 *
35
 * @implements ChildBuilderAttributeInterface<ArrayElementBuilder>
36
 *
37
 * @psalm-suppress PropertyNotSetInConstructor
38
 */
39
#[Attribute(Attribute::TARGET_PROPERTY)]
40
final class Count extends CountConstraint implements ChildBuilderAttributeInterface
41
{
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
46
    {
47 1
        $builder->arrayConstraint($this);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function validatedBy(): string
54
    {
55 1
        return CountConstraint::class . 'Validator';
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
62
    {
63 2
        $defaultParameters = get_class_vars(CountConstraint::class);
64
        /** @var array{
65
         *     minMessage?: string,
66
         *     maxMessage?: string,
67
         *     exactMessage?: string,
68
         *     divisibleByMessage?: string,
69
         *     min?: int|null,
70
         *     max?: int|null,
71
         *     divisibleBy?: int|null,
72
         * } $parameters
73
         */
74 2
        $parameters = get_object_vars($this);
75
76 2
        foreach ($parameters as $paramName => $value) {
77 2
            if (!array_key_exists($paramName, $defaultParameters) || $value === $defaultParameters[$paramName]) {
78 2
                unset($parameters[$paramName]);
79
            }
80
        }
81
82 2
        $generator->line('$?->arrayConstraint(?);', [$name, $generator->new(CountConstraint::class, $parameters)]);
83
    }
84
}
85