Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
8.00% |
2 / 25 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MultiSelectComponentProvider | |
8.00% |
2 / 25 |
|
0.00% |
0 / 2 |
24.47 | |
0.00% |
0 / 1 |
| supports | |
16.67% |
2 / 12 |
|
0.00% |
0 / 1 |
13.26 | |||
| createComponentFor | |
0.00% |
0 / 13 |
|
0.00% |
0 / 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\Utils\HashmapUtils; |
| 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 ReflectionClass; |
| 14 | use ReflectionNamedType; |
| 15 | use ReflectionType; |
| 16 | |
| 17 | class MultiSelectComponentProvider implements FormComponentProviderInterface |
| 18 | { |
| 19 | public function supports(ReflectionType $type, FormBuildContext $formBuildContext): bool |
| 20 | { |
| 21 | if (HashmapUtils::isSet($type)) { |
| 22 | $metadata = MetadataFactory::getCreationMetadata($type, $formBuildContext->getApieContext()); |
| 23 | $options = $metadata->getArrayItemType()?->getValueOptions($formBuildContext->getApieContext(), true); |
| 24 | if (empty($options)) { |
| 25 | return false; |
| 26 | } |
| 27 | $scalar = $metadata->getArrayItemType()?->toScalarType(); |
| 28 | if (!in_array($scalar, ScalarType::PRIMITIVES, true)) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | assert($type instanceof ReflectionNamedType); |
| 33 | $refl = new ReflectionClass($type->getName()); |
| 34 | return $refl->isInstantiable(); |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param ReflectionNamedType $type |
| 41 | */ |
| 42 | public function createComponentFor(ReflectionType $type, FormBuildContext $formBuildContext): ComponentInterface |
| 43 | { |
| 44 | $metadata = MetadataFactory::getCreationMetadata($type, $formBuildContext->getApieContext()); |
| 45 | $options = $metadata->getArrayItemType()->getValueOptions($formBuildContext->getApieContext(), true); |
| 46 | return new SingleInput( |
| 47 | $formBuildContext->getFormName(), |
| 48 | $formBuildContext->getFilledInValue(), |
| 49 | $formBuildContext->createTranslationLabel(), |
| 50 | $type->allowsNull(), |
| 51 | $type, |
| 52 | new CmsSingleInput( |
| 53 | ['multi'], |
| 54 | new CmsInputOption(options: $options) |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | } |