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