Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| FromItemListOrHashmap | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| support | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getIndexes | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Indexing; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Lists\ItemHashmap; |
| 6 | use Apie\Core\Lists\ItemList; |
| 7 | use Apie\Core\Lists\ItemSet; |
| 8 | use ReflectionClass; |
| 9 | |
| 10 | class FromItemListOrHashmap implements IndexingStrategyInterface |
| 11 | { |
| 12 | public function support(object $object): bool |
| 13 | { |
| 14 | return (new ReflectionClass(ItemList::class))->isInstance($object) |
| 15 | || (new ReflectionClass(ItemSet::class))->isInstance($object) |
| 16 | || (new ReflectionClass(ItemHashmap::class))->isInstance($object); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return array<string, int> |
| 21 | */ |
| 22 | public function getIndexes(object $object, ApieContext $context, Indexer $indexer): array |
| 23 | { |
| 24 | $result = []; |
| 25 | if ($object instanceof ItemList || $object instanceof ItemSet) { |
| 26 | foreach ($object as $item) { |
| 27 | $objectResult = $indexer->getIndexesFor($item, $context); |
| 28 | $result = Indexer::merge($result, $objectResult); |
| 29 | } |
| 30 | return $result; |
| 31 | } |
| 32 | assert($object instanceof ItemHashmap); |
| 33 | foreach ($object as $key => $item) { |
| 34 | $result[$key] = ($result[$key] ?? 0) + 1; |
| 35 | $objectResult = $indexer->getIndexesFor($item, $context); |
| 36 | $result = Indexer::merge($result, $objectResult); |
| 37 | } |
| 38 | return $result; |
| 39 | } |
| 40 | } |