Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| GetSearchIndexAttributeConverter | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyToDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyToStorage | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\PropertyConverters; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Indexing\Indexer; |
| 6 | use Apie\DoctrineEntityConverter\Entities\SearchIndex; |
| 7 | use Apie\StorageMetadata\Attributes\GetSearchIndexAttribute; |
| 8 | use Apie\StorageMetadata\Interfaces\PropertyConverterInterface; |
| 9 | use Apie\StorageMetadata\Mediators\DomainToStorageContext; |
| 10 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | class GetSearchIndexAttributeConverter implements PropertyConverterInterface |
| 14 | { |
| 15 | public function __construct(private readonly Indexer $indexer) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | public function applyToDomain( |
| 20 | DomainToStorageContext $context |
| 21 | ): void { |
| 22 | // no-op |
| 23 | } |
| 24 | |
| 25 | public function applyToStorage( |
| 26 | DomainToStorageContext $context |
| 27 | ): void { |
| 28 | $storageProperty = $context->storageProperty; |
| 29 | |
| 30 | foreach ($storageProperty->getAttributes(GetSearchIndexAttribute::class) as $propertyAttribute) { |
| 31 | $arrayValueType = $propertyAttribute->newInstance()->arrayValueType; |
| 32 | if ($arrayValueType && str_starts_with($arrayValueType, 'apie_')) { |
| 33 | $arrayValueType = (new ReflectionClass($context->storageObject))->getNamespaceName() . '\\' . $arrayValueType; |
| 34 | } |
| 35 | $domainPropertyValue = $propertyAttribute->newInstance()->getValue($context->domainClass, $context->domainObject); |
| 36 | $storagePropertyType = $storageProperty->getType(); |
| 37 | if ($domainPropertyValue === null) { |
| 38 | $indexes = []; |
| 39 | } elseif (is_bool($domainPropertyValue)) { |
| 40 | $indexes = [$domainPropertyValue ? '1' : '0']; |
| 41 | } elseif (is_object($domainPropertyValue)) { |
| 42 | $indexes = array_keys($this->indexer->getIndexesForObject($domainPropertyValue, new ApieContext())); |
| 43 | } elseif (is_resource($domainPropertyValue)) { |
| 44 | $indexes = []; |
| 45 | } elseif (get_debug_type($domainPropertyValue) === 'resource (closed)') { |
| 46 | $indexes = []; |
| 47 | } else { |
| 48 | $indexes = [$context->dynamicCast($domainPropertyValue, ReflectionTypeFactory::createReflectionType('string'))]; |
| 49 | } |
| 50 | if ($arrayValueType) { |
| 51 | $t = ReflectionTypeFactory::createReflectionType($arrayValueType); |
| 52 | $indexes = array_map(function ($index) use ($context, $t) { |
| 53 | $result = $context->dynamicCast(substr((string) $index, 0, 255), $t); |
| 54 | if ($result instanceof SearchIndex) { |
| 55 | $result->parent = $context->storageObject; |
| 56 | } |
| 57 | return $result; |
| 58 | }, $indexes); |
| 59 | } |
| 60 | $storagePropertyValue = $context->dynamicCast($indexes, $storagePropertyType); |
| 61 | $context->setStoragePropertyValue($storagePropertyValue); |
| 62 | } |
| 63 | } |
| 64 | } |