Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.00% covered (danger)
8.00%
2 / 25
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiSelectComponentProvider
8.00% covered (danger)
8.00%
2 / 25
0.00% covered (danger)
0.00%
0 / 2
24.47
0.00% covered (danger)
0.00%
0 / 1
 supports
16.67% covered (danger)
16.67%
2 / 12
0.00% covered (danger)
0.00%
0 / 1
13.26
 createComponentFor
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\HtmlBuilders\Factories\Concrete;
3
4use Apie\Core\Attributes\CmsSingleInput;
5use Apie\Core\Dto\CmsInputOption;
6use Apie\Core\Enums\ScalarType;
7use Apie\Core\Metadata\MetadataFactory;
8use Apie\Core\Utils\HashmapUtils;
9use Apie\HtmlBuilders\Components\Forms\SingleInput;
10use Apie\HtmlBuilders\FormBuildContext;
11use Apie\HtmlBuilders\Interfaces\ComponentInterface;
12use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface;
13use ReflectionClass;
14use ReflectionNamedType;
15use ReflectionType;
16
17class 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}