Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
38 / 38 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Indexer | |
100.00% |
38 / 38 |
|
100.00% |
5 / 5 |
18 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getIndexesFor | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
11 | |||
| getIndexesForObject | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| merge | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Indexing; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\CountWords\WordCounter; |
| 6 | |
| 7 | final class Indexer |
| 8 | { |
| 9 | /** |
| 10 | * @var array<string, IndexingStrategyInterface> |
| 11 | */ |
| 12 | private array $strategies; |
| 13 | |
| 14 | public function __construct(IndexingStrategyInterface... $strategies) |
| 15 | { |
| 16 | $this->strategies = $strategies; |
| 17 | } |
| 18 | |
| 19 | public static function create(): self |
| 20 | { |
| 21 | return new self( |
| 22 | new FromAttribute(), |
| 23 | new FromUploadedFile(), |
| 24 | new FromDateObject(), |
| 25 | new FromPhoneNumber(), |
| 26 | new SkipPasswordFields(), |
| 27 | new FromEnum(), |
| 28 | new FromItemListOrHashmap(), |
| 29 | new FromValueObject(), |
| 30 | new FromGetters() |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return array<string, int> |
| 36 | */ |
| 37 | public function getIndexesFor(mixed $instance, ApieContext $apieContext): array |
| 38 | { |
| 39 | if (is_object($instance)) { |
| 40 | return $this->getIndexesForObject($instance, $apieContext); |
| 41 | } |
| 42 | if (is_array($instance)) { |
| 43 | $result = []; |
| 44 | foreach ($instance as $key => $item) { |
| 45 | $result[$key] = ($result[$key] ?? 0) + 1; |
| 46 | $objectResult = $this->getIndexesFor($item, $apieContext); |
| 47 | $result = Indexer::merge($result, $objectResult); |
| 48 | } |
| 49 | return $result; |
| 50 | } |
| 51 | if ($instance === null) { |
| 52 | return []; |
| 53 | } |
| 54 | if (is_string($instance)) { |
| 55 | return WordCounter::countFromString($instance); |
| 56 | } |
| 57 | return match (get_debug_type($instance)) { |
| 58 | 'int' => [$instance => 1], |
| 59 | 'float' => [((string)$instance) => 1], |
| 60 | 'bool' => $instance ? ['1' => 1] : ['0' => 1], |
| 61 | default => [], |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return array<string, int> |
| 67 | */ |
| 68 | public function getIndexesForObject(object $entity, ApieContext $apieContext): array |
| 69 | { |
| 70 | foreach ($this->strategies as $strategy) { |
| 71 | if ($strategy->support($entity)) { |
| 72 | return $strategy->getIndexes($entity, $apieContext, $this); |
| 73 | } |
| 74 | } |
| 75 | return []; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param array<string, int> $input1 |
| 80 | * @param array<string, int> $input2 |
| 81 | * @return array<string, int> |
| 82 | */ |
| 83 | public static function merge(array $input1, array $input2): array |
| 84 | { |
| 85 | foreach ($input2 as $value => $prio) { |
| 86 | $input1[$value] = ($input1[$value] ?? 0) + $prio; |
| 87 | } |
| 88 | |
| 89 | return $input1; |
| 90 | } |
| 91 | } |