Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PropertyAttributeConverter | |
95.24% |
20 / 21 |
|
66.67% |
2 / 3 |
12 | |
0.00% |
0 / 1 |
| getPropertyAttributes | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| applyToDomain | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
8.12 | |||
| applyToStorage | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\PropertyConverters; |
| 3 | |
| 4 | use Apie\Core\Attributes\Optional; |
| 5 | use Apie\StorageMetadata\Attributes\DecimalPropertyAttribute; |
| 6 | use Apie\StorageMetadata\Attributes\PropertyAttribute; |
| 7 | use Apie\StorageMetadata\Interfaces\PropertyConverterInterface; |
| 8 | use Apie\StorageMetadata\Mediators\DomainToStorageContext; |
| 9 | |
| 10 | class PropertyAttributeConverter implements PropertyConverterInterface |
| 11 | { |
| 12 | /** |
| 13 | * @return array<\ReflectionAttribute<PropertyAttribute>> |
| 14 | */ |
| 15 | private function getPropertyAttributes(DomainToStorageContext $context): array |
| 16 | { |
| 17 | $storageProperty = $context->storageProperty; |
| 18 | return [ |
| 19 | ...$storageProperty->getAttributes(PropertyAttribute::class), |
| 20 | ...$storageProperty->getAttributes(DecimalPropertyAttribute::class), |
| 21 | ]; |
| 22 | } |
| 23 | |
| 24 | public function applyToDomain( |
| 25 | DomainToStorageContext $context |
| 26 | ): void { |
| 27 | foreach ($this->getPropertyAttributes($context) as $propertyAttribute) { |
| 28 | $domainProperty = $propertyAttribute->newInstance()->getReflectionProperty($context->domainClass, $context->domainObject); |
| 29 | if ($domainProperty && (!$domainProperty->isInitialized($context->domainObject) || !$domainProperty->isReadOnly())) { |
| 30 | $domainPropertyType = $domainProperty->getType(); |
| 31 | $domainPropertyValue = $context->dynamicCast($context->getStoragePropertyValue(), $domainPropertyType); |
| 32 | if (!$domainPropertyType->allowsNull() && $domainPropertyValue === null && $domainProperty->getAttributes(Optional::class)) { |
| 33 | continue; |
| 34 | } |
| 35 | $domainProperty->setValue($context->domainObject, $domainPropertyValue); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function applyToStorage( |
| 41 | DomainToStorageContext $context |
| 42 | ): void { |
| 43 | $storageProperty = $context->storageProperty; |
| 44 | |
| 45 | foreach ($this->getPropertyAttributes($context) as $propertyAttribute) { |
| 46 | $domainProperty = $propertyAttribute->newInstance()->getReflectionProperty($context->domainClass, $context->domainObject); |
| 47 | if ($domainProperty) { |
| 48 | $storagePropertyType = $storageProperty->getType(); |
| 49 | $domainPropertyValue = $domainProperty->getValue($context->domainObject); |
| 50 | $storagePropertyValue = $context->dynamicCast($domainPropertyValue, $storagePropertyType); |
| 51 | $context->setStoragePropertyValue($storagePropertyValue); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |