Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.67% covered (danger)
46.67%
7 / 15
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseComponent
46.67% covered (danger)
46.67%
7 / 15
66.67% covered (warning)
66.67%
4 / 6
21.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getComponent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMissingValidationErrors
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makePrototype
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 withName
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2namespace Apie\HtmlBuilders\Components;
3
4use Apie\HtmlBuilders\FormBuildContext;
5use Apie\HtmlBuilders\Interfaces\ComponentInterface;
6use Apie\HtmlBuilders\Lists\ComponentHashmap;
7use Apie\HtmlBuilders\ValueObjects\FormName;
8
9abstract class BaseComponent implements ComponentInterface
10{
11    protected ComponentHashmap $childComponents;
12
13    /** @param array<string|int, mixed> $attributes */
14    public function __construct(protected array $attributes, ?ComponentHashmap $childComponents = null)
15    {
16        $this->childComponents = $childComponents ?? new ComponentHashmap();
17    }
18
19    public function getComponent(string $key): ComponentInterface
20    {
21        return $this->childComponents[$key];
22    }
23
24    public function getMissingValidationErrors(FormBuildContext $formBuildContext): array
25    {
26        return $formBuildContext->getMissingValidationErrors($this->childComponents->toArray());
27    }
28
29    public function getAttribute(string $key): mixed
30    {
31        return $this->attributes[$key] ?? null;
32    }
33
34    final public function makePrototype(string $prototypeName, BaseComponent $component): BaseComponent
35    {
36        $component->attributes['additionalAttributes'] ??= [];
37        $component->attributes['additionalAttributes']['id'] = $prototypeName;
38        $component->attributes['additionalAttributes']['prototyped'] = 'prototyped';
39        return $component;
40    }
41
42    public function withName(FormName $name, mixed $value = null): ComponentInterface
43    {
44        $item = clone $this;
45        $item->attributes['name'] = $name;
46        if ($value !== null || isset($item->attributes['value'])) {
47            $item->attributes['value'] = $value;
48        }
49        foreach ($this->childComponents as $childComponentKey => $childComponent) {
50            $item->childComponents[$childComponentKey] = $childComponent->withName($name->createChildForm($childComponentKey));
51        }
52        return $item;
53    }
54}