Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SegmentDisplayProvider | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| createComponentFor | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\FieldDisplayProviders; |
| 3 | |
| 4 | use Apie\Core\Enums\ScalarType; |
| 5 | use Apie\Core\Metadata\GetterInterface; |
| 6 | use Apie\Core\Metadata\MetadataFactory; |
| 7 | use Apie\HtmlBuilders\Components\Resource\FieldDisplay\SegmentDisplay; |
| 8 | use Apie\HtmlBuilders\FieldDisplayBuildContext; |
| 9 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 10 | use Apie\HtmlBuilders\Interfaces\FieldDisplayComponentProviderInterface; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | class SegmentDisplayProvider implements FieldDisplayComponentProviderInterface |
| 14 | { |
| 15 | public function supports(mixed $object, FieldDisplayBuildContext $context): bool |
| 16 | { |
| 17 | if (!is_object($object)) { |
| 18 | return false; |
| 19 | } |
| 20 | $metadata = MetadataFactory::getResultMetadata( |
| 21 | new ReflectionClass($object), |
| 22 | $context->getApieContext() |
| 23 | ); |
| 24 | return $metadata->toScalarType() === ScalarType::STDCLASS; |
| 25 | } |
| 26 | |
| 27 | public function createComponentFor(mixed $object, FieldDisplayBuildContext $context): ComponentInterface |
| 28 | { |
| 29 | $metadata = MetadataFactory::getResultMetadata( |
| 30 | new ReflectionClass($object), |
| 31 | $context->getApieContext() |
| 32 | ); |
| 33 | $nodes = $context->getVisitedNodes(); |
| 34 | $prefix = empty($nodes) ? '' : (implode('.', $nodes) . '.'); |
| 35 | $detailComponents = []; |
| 36 | foreach ($metadata->getHashmap()->filterOnContext($context->getApieContext()) as $propertyName => $fieldMetadata) { |
| 37 | if ($fieldMetadata instanceof GetterInterface) { |
| 38 | $propertyContext = $context->createChildContext($propertyName); |
| 39 | $detailComponents[$prefix . $propertyName] = $propertyContext->createComponentFor( |
| 40 | $fieldMetadata->getValue($object, $propertyContext->getApieContext()) |
| 41 | ); |
| 42 | } |
| 43 | } |
| 44 | return new SegmentDisplay($detailComponents); |
| 45 | } |
| 46 | } |