Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.05% |
27 / 38 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UnionTypehintComponentProvider | |
71.05% |
27 / 38 |
|
66.67% |
2 / 3 |
10.96 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createComponentFor | |
65.62% |
21 / 32 |
|
0.00% |
0 / 1 |
7.46 | |||
| getSafePanelName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 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\Enums\ScalarType; |
| 7 | use Apie\Core\Metadata\MetadataFactory; |
| 8 | use Apie\Core\Metadata\Strategy\UnionTypeStrategy; |
| 9 | use Apie\HtmlBuilders\Components\Forms\FormSplit; |
| 10 | use Apie\HtmlBuilders\Components\Forms\SingleInput; |
| 11 | use Apie\HtmlBuilders\FormBuildContext; |
| 12 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 13 | use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface; |
| 14 | use Apie\HtmlBuilders\Lists\ComponentHashmap; |
| 15 | use ReflectionType; |
| 16 | use ReflectionUnionType; |
| 17 | |
| 18 | class UnionTypehintComponentProvider implements FormComponentProviderInterface |
| 19 | { |
| 20 | public function supports(ReflectionType $type, FormBuildContext $context): bool |
| 21 | { |
| 22 | return $type instanceof ReflectionUnionType; |
| 23 | } |
| 24 | |
| 25 | public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface |
| 26 | { |
| 27 | assert($type instanceof ReflectionUnionType); |
| 28 | $formComponentFactory = $context->getComponentFactory(); |
| 29 | $strategy = MetadataFactory::getMetadataStrategyForType($type); |
| 30 | if ($strategy instanceof UnionTypeStrategy) { |
| 31 | $metadata = $strategy->getCreationMetadata($context->getApieContext()); |
| 32 | $scalar = $metadata->toScalarType(true); |
| 33 | if (!in_array($scalar, [ScalarType::ARRAY, ScalarType::STDCLASS, ScalarType::MIXED])) { |
| 34 | $options = $metadata->getValueOptions($context->getApieContext(), true); |
| 35 | if ($options) { |
| 36 | return new SingleInput( |
| 37 | $context->getFormName(), |
| 38 | $context->getFilledInValue(), |
| 39 | $context->createTranslationLabel(), |
| 40 | $type->allowsNull(), |
| 41 | $type, |
| 42 | new CmsSingleInput( |
| 43 | ['select'], |
| 44 | new CmsInputOption(options: $options) |
| 45 | ) |
| 46 | ); |
| 47 | } |
| 48 | return $formComponentFactory->createFromType($scalar->toReflectionType(), $context); |
| 49 | } |
| 50 | } |
| 51 | $components = []; |
| 52 | foreach ($type->getTypes() as $subType) { |
| 53 | $key = $this->getSafePanelName($subType); |
| 54 | $components[$key] = $formComponentFactory->createFromType($subType, $context); |
| 55 | } |
| 56 | return new FormSplit( |
| 57 | $context->getFormName(), |
| 58 | false, |
| 59 | false, |
| 60 | $context->getFilledInValue($type->allowsNull() ? null : ''), |
| 61 | new ComponentHashmap($components) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | public function getSafePanelName(ReflectionType $type): string |
| 66 | { |
| 67 | $type = (string) $type; |
| 68 | $pos = strrpos($type, '\\'); |
| 69 | if ($pos !== false) { |
| 70 | $type = substr($type, $pos + 1); |
| 71 | } |
| 72 | return preg_replace('/[^A-Za-z0-9]/', '_', $type); |
| 73 | } |
| 74 | } |