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