Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.06% |
2 / 33 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ListDisplayProvider | |
6.06% |
2 / 33 |
|
50.00% |
2 / 4 |
176.48 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isSimpleList | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| createComponentFor | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\FieldDisplayProviders; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Enums\ScalarType; |
| 6 | use Apie\Core\Lists\ItemList; |
| 7 | use Apie\Core\Lists\ItemSet; |
| 8 | use Apie\Core\Metadata\MetadataFactory; |
| 9 | use Apie\Core\Utils\ConverterUtils; |
| 10 | use Apie\Core\Utils\HashmapUtils; |
| 11 | use Apie\Core\ValueObjects\Utils; |
| 12 | use Apie\HtmlBuilders\Columns\ColumnSelector; |
| 13 | use Apie\HtmlBuilders\Components\Resource\FieldDisplay\ListDisplay; |
| 14 | use Apie\HtmlBuilders\Components\Resource\FieldDisplay\SegmentDisplay; |
| 15 | use Apie\HtmlBuilders\FieldDisplayBuildContext; |
| 16 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 17 | use Apie\HtmlBuilders\Interfaces\FieldDisplayComponentProviderInterface; |
| 18 | use Apie\Serializer\Serializer; |
| 19 | use ReflectionClass; |
| 20 | |
| 21 | final class ListDisplayProvider implements FieldDisplayComponentProviderInterface |
| 22 | { |
| 23 | public function __construct( |
| 24 | private readonly ColumnSelector $columnSelector |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | public function supports(mixed $object, FieldDisplayBuildContext $context): bool |
| 29 | { |
| 30 | return $object instanceof ItemList || $object instanceof ItemSet; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param ReflectionClass<ItemList|ItemSet> $refl |
| 35 | */ |
| 36 | private function isSimpleList(ReflectionClass $refl, ApieContext $apieContext): bool |
| 37 | { |
| 38 | if (!$apieContext->hasContext(Serializer::class)) { |
| 39 | return false; |
| 40 | } |
| 41 | $columns = $this->columnSelector->getColumns($refl, $apieContext); |
| 42 | $display = MetadataFactory::getResultMetadata($refl, $apieContext); |
| 43 | $noMatchingColumns = true; |
| 44 | foreach ($display->getHashmap() as $fieldName => $fieldMetadata) { |
| 45 | $typehint = $fieldMetadata->getTypehint(); |
| 46 | $scalar = MetadataFactory::getScalarForType($typehint, true); |
| 47 | if (!in_array($scalar, ScalarType::PRIMITIVES)) { |
| 48 | return false; |
| 49 | } |
| 50 | if (in_array($fieldName, $columns)) { |
| 51 | $noMatchingColumns = false; |
| 52 | } |
| 53 | } |
| 54 | return !$noMatchingColumns; |
| 55 | } |
| 56 | |
| 57 | public function createComponentFor(mixed $object, FieldDisplayBuildContext $context): ComponentInterface |
| 58 | { |
| 59 | assert($object instanceof ItemList || $object instanceof ItemSet); |
| 60 | $apieContext = $context->getApieContext(); |
| 61 | $refl = new ReflectionClass($object); |
| 62 | $arrayType = HashmapUtils::getArrayType($refl); |
| 63 | $arrayTypeClass = ConverterUtils::toReflectionClass($arrayType); |
| 64 | $scalar = MetadataFactory::getScalarForType($arrayType, true); |
| 65 | if ($arrayTypeClass && !in_array($scalar, ScalarType::PRIMITIVES) && $this->isSimpleList($refl, $apieContext)) { |
| 66 | $serializer = $apieContext->getContext(Serializer::class); |
| 67 | assert($serializer instanceof Serializer); |
| 68 | |
| 69 | return new ListDisplay( |
| 70 | Utils::toArray($serializer->normalize($object, $apieContext)), |
| 71 | $this->columnSelector->getColumns($arrayTypeClass, $apieContext) |
| 72 | ); |
| 73 | } |
| 74 | /** @var array<string, mixed> $detailComponents */ |
| 75 | $detailComponents = []; |
| 76 | $childContext = $context->createChildContext('_'); |
| 77 | foreach ($object as $key => $value) { |
| 78 | $detailComponents[$key] = $childContext->createComponentFor($value); |
| 79 | } |
| 80 | return new SegmentDisplay($detailComponents, showKeys: false); |
| 81 | } |
| 82 | } |