Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| FromValueObject | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
5.02 | |
0.00% |
0 / 1 |
| support | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| getIndexes | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Indexing; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 6 | use ReflectionClass; |
| 7 | use ReflectionNamedType; |
| 8 | use Stringable; |
| 9 | |
| 10 | class FromValueObject implements IndexingStrategyInterface |
| 11 | { |
| 12 | public function support(object $object): bool |
| 13 | { |
| 14 | $refl = new ReflectionClass($object); |
| 15 | if (!$refl->implementsInterface(ValueObjectInterface::class)) { |
| 16 | return false; |
| 17 | } |
| 18 | $type = $refl->getMethod('toNative')->getReturnType(); |
| 19 | if ($type instanceof ReflectionNamedType) { |
| 20 | return $type->getName() === 'string' || $type->getName() === '?string'; |
| 21 | } |
| 22 | |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @return array<string, int> |
| 28 | */ |
| 29 | public function getIndexes(object $object, ApieContext $context, Indexer $indexer): array |
| 30 | { |
| 31 | assert($object instanceof ValueObjectInterface); |
| 32 | assert($object instanceof Stringable); |
| 33 | $value = (string) $object; |
| 34 | return [$value => 1]; |
| 35 | } |
| 36 | } |