Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
18 / 21
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemListComponentProvider
85.71% covered (warning)
85.71%
18 / 21
50.00% covered (danger)
50.00%
1 / 2
5.07
0.00% covered (danger)
0.00%
0 / 1
 supports
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
 createComponentFor
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\HtmlBuilders\Factories\Concrete;
3
4use Apie\Core\Utils\HashmapUtils;
5use Apie\HtmlBuilders\Components\Forms\FormPrototypeList;
6use Apie\HtmlBuilders\FormBuildContext;
7use Apie\HtmlBuilders\Interfaces\ComponentInterface;
8use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface;
9use ReflectionClass;
10use ReflectionNamedType;
11use ReflectionType;
12
13class ItemListComponentProvider implements FormComponentProviderInterface
14{
15    public function supports(ReflectionType $type, FormBuildContext $formBuildContext): bool
16    {
17        if (HashmapUtils::isList($type)) {
18            assert($type instanceof ReflectionNamedType);
19            $refl = new ReflectionClass($type->getName());
20            return $refl->isInstantiable();
21        }
22        if (HashmapUtils::isSet($type)) {
23            assert($type instanceof ReflectionNamedType);
24            $refl = new ReflectionClass($type->getName());
25            return $refl->isInstantiable();
26        }
27        return false;
28    }
29
30    /**
31     * @param ReflectionNamedType $type
32     */
33    public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface
34    {
35        $formComponentFactory = $context->getComponentFactory();
36        $refl = new ReflectionClass($type->getName());
37        $prototypeName = $context->getFormName()->getPrototypeName();
38        return new FormPrototypeList(
39            $context->getFormName(),
40            $context->getFilledInValue($type->allowsNull() ? null : []),
41            $prototypeName,
42            $formComponentFactory->createFromType(
43                $refl->getMethod('offsetGet')->getReturnType(),
44                $context->createChildContext($prototypeName)
45            )
46        );
47    }
48}