Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| EntityComponentProvider | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
| createComponentFor | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\Factories\Concrete; |
| 3 | |
| 4 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 5 | use Apie\Core\Metadata\CompositeMetadata; |
| 6 | use Apie\Core\Metadata\MetadataFactory; |
| 7 | use Apie\HtmlBuilders\FormBuildContext; |
| 8 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 9 | use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionNamedType; |
| 12 | use ReflectionType; |
| 13 | |
| 14 | class EntityComponentProvider implements FormComponentProviderInterface |
| 15 | { |
| 16 | public function supports(ReflectionType $type, FormBuildContext $context): bool |
| 17 | { |
| 18 | if ($type instanceof ReflectionNamedType && !$type->isBuiltin() && class_exists($type->getName())) { |
| 19 | $refl = new ReflectionClass($type->getName()); |
| 20 | return $refl->isInstantiable() |
| 21 | && !$refl->implementsInterface(PolymorphicEntityInterface::class) |
| 22 | && MetadataFactory::getCreationMetadata($type, $context->getApieContext()) instanceof CompositeMetadata |
| 23 | ; |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param ReflectionNamedType $type |
| 30 | */ |
| 31 | public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface |
| 32 | { |
| 33 | $formComponentFactory = $context->getComponentFactory(); |
| 34 | return $formComponentFactory->createFromClass( |
| 35 | new ReflectionClass($type->getName()), |
| 36 | $context, |
| 37 | $this |
| 38 | ); |
| 39 | } |
| 40 | } |