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