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