Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FromAttribute
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 support
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getIndexes
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\Core\Indexing;
3
4use Apie\Core\Attributes\ProvideIndex;
5use Apie\Core\Context\ApieContext;
6use ReflectionAttribute;
7use ReflectionClass;
8
9class FromAttribute implements IndexingStrategyInterface
10{
11    public function support(object $object): bool
12    {
13        $refl = new ReflectionClass($object);
14        return ! empty($refl->getAttributes(ProvideIndex::class));
15    }
16
17    /**
18     * @return array<string, int>
19     */
20    public function getIndexes(object $object, ApieContext $context, Indexer $indexer): array
21    {
22        $refl = new ReflectionClass($object);
23        $result = [];
24        $attributes = $refl->getAttributes(ProvideIndex::class);
25        foreach ($attributes as $attribute) {
26            /** @var ReflectionAttribute<ProvideIndex> $attribute */
27            $method = $refl->getMethod($attribute->newInstance()->methodName);
28            $result += $method->invoke($object);
29        }
30        return $result;
31    }
32}