Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
24 / 40 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FromGetters | |
60.00% |
24 / 40 |
|
33.33% |
1 / 3 |
42.10 | |
0.00% |
0 / 1 |
| support | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getIndexes | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
8.02 | |||
| getPrio | |
34.78% |
8 / 23 |
|
0.00% |
0 / 1 |
31.47 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Indexing; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Dto\DtoInterface; |
| 6 | use Apie\Core\Entities\EntityInterface; |
| 7 | use Apie\Core\Metadata\Fields\FieldInterface; |
| 8 | use Apie\Core\Metadata\GetterInterface; |
| 9 | use Apie\Core\Metadata\MetadataFactory; |
| 10 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 11 | use ReflectionClass; |
| 12 | use ReflectionNamedType; |
| 13 | |
| 14 | class FromGetters implements IndexingStrategyInterface |
| 15 | { |
| 16 | public function support(object $object): bool |
| 17 | { |
| 18 | return $object instanceof EntityInterface || $object instanceof DtoInterface; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return array<string, int> |
| 23 | */ |
| 24 | public function getIndexes(object $object, ApieContext $context, Indexer $indexer): array |
| 25 | { |
| 26 | $metadata = MetadataFactory::getResultMetadata(new ReflectionClass($object), $context); |
| 27 | $result = []; |
| 28 | foreach ($metadata->getHashmap() as $propertyName => $fieldMetadata) { |
| 29 | if (!$fieldMetadata->isField() || !$fieldMetadata instanceof GetterInterface) { |
| 30 | continue; |
| 31 | } |
| 32 | /** @var GetterInterface&FieldInterface $fieldMetadata */ |
| 33 | $typehint = $fieldMetadata->getTypehint(); |
| 34 | if (!$typehint instanceof ReflectionNamedType) { |
| 35 | continue; |
| 36 | } |
| 37 | $value = $fieldMetadata->getValue($object, $context); |
| 38 | if (is_object($value)) { |
| 39 | $embeddedObjectResult = $indexer->getIndexesForObject($value, $context); |
| 40 | $result = Indexer::merge($result, $embeddedObjectResult); |
| 41 | } elseif (is_string($value) || is_numeric($value)) { |
| 42 | $value = (string) $value; |
| 43 | $result[$value] = $this->getPrio($propertyName, $typehint, $fieldMetadata, $value, $result[$value] ?? 0); |
| 44 | } |
| 45 | } |
| 46 | return $result; |
| 47 | } |
| 48 | |
| 49 | private function getPrio( |
| 50 | string $propertyName, |
| 51 | ReflectionNamedType $typehint, |
| 52 | FieldInterface&GetterInterface $fieldMetadata, |
| 53 | string $value, |
| 54 | int $currentPrio |
| 55 | ): int { |
| 56 | if (in_array($typehint->getName(), ['string', 'int', 'float'])) { |
| 57 | return max( |
| 58 | match ($propertyName) { |
| 59 | 'id' => 1, |
| 60 | 'name' => 5, |
| 61 | 'description' => 4, |
| 62 | default => 2, |
| 63 | }, |
| 64 | $currentPrio |
| 65 | ); |
| 66 | } elseif (!$typehint->isBuiltin() && class_exists($typehint->getName())) { |
| 67 | $refl = new ReflectionClass($typehint->getName()); |
| 68 | if ($refl->implementsInterface(ValueObjectInterface::class)) { |
| 69 | $returnType = $refl->getMethod('toNative')->getReturnType(); |
| 70 | assert($returnType instanceof ReflectionNamedType); |
| 71 | return $this->getPrio( |
| 72 | $propertyName, |
| 73 | $returnType, |
| 74 | $fieldMetadata, |
| 75 | $value, |
| 76 | $currentPrio |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | } |