Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
42.86% |
6 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
HideUuidAsIdComponentProvider | |
42.86% |
6 / 14 |
|
0.00% |
0 / 2 |
19.94 | |
0.00% |
0 / 1 |
supports | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
6.10 | |||
createComponentFor | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | namespace Apie\HtmlBuilders\Factories\Concrete; |
3 | |
4 | use Apie\Core\Identifiers\Uuid; |
5 | use Apie\Core\Identifiers\UuidV4; |
6 | use Apie\Core\ValueObjects\Utils; |
7 | use Apie\HtmlBuilders\Components\Forms\HiddenField; |
8 | use Apie\HtmlBuilders\FormBuildContext; |
9 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
10 | use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface; |
11 | use ReflectionNamedType; |
12 | use ReflectionType; |
13 | |
14 | class HideUuidAsIdComponentProvider implements FormComponentProviderInterface |
15 | { |
16 | public function supports(ReflectionType $type, FormBuildContext $context): bool |
17 | { |
18 | if ( |
19 | !$type instanceof ReflectionNamedType |
20 | || $type->allowsNull() |
21 | || $type->isBuiltIn() |
22 | || !$context->getFormName()->hasChildFormFieldName() |
23 | || $context->getFormName()->getChildFormFieldName() !== 'id' |
24 | ) { |
25 | return false; |
26 | } |
27 | return is_subclass_of($type->getName(), Uuid::class, true); |
28 | } |
29 | |
30 | public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface |
31 | { |
32 | $id = Utils::toString($context->getFilledInValue('', true)); |
33 | if (!preg_match(Uuid::getRegularExpression(), $id)) { |
34 | $id = UuidV4::createRandom()->toNative(); |
35 | }; |
36 | return new HiddenField( |
37 | $context->getFormName(), |
38 | $id |
39 | ); |
40 | } |
41 | } |