Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.86% covered (danger)
42.86%
6 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HideUuidAsIdComponentProvider
42.86% covered (danger)
42.86%
6 / 14
0.00% covered (danger)
0.00%
0 / 2
19.94
0.00% covered (danger)
0.00%
0 / 1
 supports
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
 createComponentFor
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\HtmlBuilders\Factories\Concrete;
3
4use Apie\Core\Identifiers\Uuid;
5use Apie\Core\Identifiers\UuidV4;
6use Apie\Core\ValueObjects\Utils;
7use Apie\HtmlBuilders\Components\Forms\HiddenField;
8use Apie\HtmlBuilders\FormBuildContext;
9use Apie\HtmlBuilders\Interfaces\ComponentInterface;
10use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface;
11use ReflectionNamedType;
12use ReflectionType;
13
14class 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}