Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.77% |
42 / 52 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
| DomainToStorageContext | |
80.77% |
42 / 52 |
|
66.67% |
6 / 9 |
30.81 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromContext | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| withStorageProperty | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withDomainObject | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withArrayKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setStoragePropertyValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStoragePropertyValue | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| dynamicCast | |
63.64% |
14 / 22 |
|
0.00% |
0 / 1 |
21.13 | |||
| clone | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Mediators; |
| 3 | |
| 4 | use Apie\Core\FileStorage\FileStorageInterface; |
| 5 | use Apie\Core\TypeUtils; |
| 6 | use Apie\Core\Utils\ConverterUtils; |
| 7 | use Apie\StorageMetadata\DomainToStorageConverter; |
| 8 | use Apie\StorageMetadata\Exceptions\CouldNotCastPropertyException; |
| 9 | use Apie\StorageMetadata\Interfaces\StorageDtoInterface; |
| 10 | use Apie\StorageMetadataBuilder\Interfaces\MixedStorageInterface; |
| 11 | use Apie\TypeConverter\TypeConverter; |
| 12 | use ReflectionClass; |
| 13 | use ReflectionNamedType; |
| 14 | use ReflectionProperty; |
| 15 | use ReflectionType; |
| 16 | use Throwable; |
| 17 | |
| 18 | final class DomainToStorageContext |
| 19 | { |
| 20 | /** |
| 21 | * @var ReflectionClass<object> |
| 22 | */ |
| 23 | public readonly ?ReflectionClass $domainClass; |
| 24 | |
| 25 | public readonly ReflectionProperty $storageProperty; |
| 26 | |
| 27 | public readonly int|string $arrayKey; |
| 28 | |
| 29 | public readonly ?DomainToStorageContext $parentContext; |
| 30 | |
| 31 | /** |
| 32 | * @template T of object |
| 33 | * @param T $domainObject |
| 34 | * @param ReflectionClass<T>|null $domainClass |
| 35 | */ |
| 36 | public function __construct( |
| 37 | public readonly DomainToStorageConverter $domainToStorageConverter, |
| 38 | public readonly TypeConverter $typeConverter, |
| 39 | public readonly StorageDtoInterface $storageObject, |
| 40 | public readonly object $domainObject, |
| 41 | public readonly FileStorageInterface $fileStorage, |
| 42 | ?ReflectionClass $domainClass = null |
| 43 | ) { |
| 44 | $this->domainClass = $domainClass ?? new ReflectionClass($domainObject); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @template T of object |
| 49 | * @param T $domainObject |
| 50 | * @param ReflectionClass<T>|null $domainClass |
| 51 | */ |
| 52 | public static function createFromContext( |
| 53 | DomainToStorageConverter $domainToStorageConverter, |
| 54 | TypeConverter $typeConverter, |
| 55 | StorageDtoInterface $storageObject, |
| 56 | object $domainObject, |
| 57 | FileStorageInterface $fileStorage, |
| 58 | ?ReflectionClass $domainClass = null, |
| 59 | ?DomainToStorageContext $context = null, |
| 60 | ): self { |
| 61 | $res = new self( |
| 62 | $domainToStorageConverter, |
| 63 | $typeConverter, |
| 64 | $storageObject, |
| 65 | $domainObject, |
| 66 | $fileStorage, |
| 67 | $domainClass |
| 68 | ); |
| 69 | $fields = [ |
| 70 | 'parentContext' => $context, |
| 71 | ]; |
| 72 | |
| 73 | if (isset($context->arrayKey)) { |
| 74 | $fields['arrayKey'] = $context->arrayKey; |
| 75 | } |
| 76 | |
| 77 | return $res->clone($fields); |
| 78 | } |
| 79 | |
| 80 | public function withStorageProperty(ReflectionProperty $storageProperty): self |
| 81 | { |
| 82 | // we do this to throw an exception in case an incorrect property is entered here. |
| 83 | $storageProperty->isInitialized($this->storageObject); |
| 84 | return $this->clone(['storageProperty' => $storageProperty]); |
| 85 | } |
| 86 | |
| 87 | public function withDomainObject(object $object): self |
| 88 | { |
| 89 | return $this->clone(['domainObject' => $object]); |
| 90 | } |
| 91 | |
| 92 | public function withArrayKey(string|int $key): self |
| 93 | { |
| 94 | return $this->clone(['arrayKey' => $key]); |
| 95 | } |
| 96 | |
| 97 | public function setStoragePropertyValue(mixed $value): void |
| 98 | { |
| 99 | $this->storageProperty->setValue($this->storageObject, $value); |
| 100 | } |
| 101 | |
| 102 | public function getStoragePropertyValue(): mixed |
| 103 | { |
| 104 | if (!$this->storageProperty->isInitialized($this->storageObject)) { |
| 105 | return $this->storageProperty->hasDefaultValue() ? $this->storageProperty->getDefaultValue() : null; |
| 106 | } |
| 107 | return $this->storageProperty->getValue($this->storageObject); |
| 108 | } |
| 109 | |
| 110 | public function dynamicCast(mixed $input, ?ReflectionType $wantedType): mixed |
| 111 | { |
| 112 | if ($input instanceof MixedStorageInterface) { |
| 113 | $input = $input->toOriginalObject(); |
| 114 | } |
| 115 | if (!$wantedType || ('mixed' === (string) $wantedType)) { |
| 116 | return $input; |
| 117 | } |
| 118 | if ($input === null && $wantedType->allowsNull()) { |
| 119 | return null; |
| 120 | } |
| 121 | if (is_object($input)) { |
| 122 | $class = ConverterUtils::toReflectionClass($wantedType); |
| 123 | if ($class && $class->isInstance($input)) { |
| 124 | return $input; |
| 125 | } |
| 126 | } elseif ($wantedType instanceof ReflectionNamedType && $wantedType->getName() === get_debug_type($input)) { |
| 127 | return $input; |
| 128 | } elseif (TypeUtils::matchesType($wantedType, $input)) { |
| 129 | return $input; |
| 130 | } |
| 131 | try { |
| 132 | return $this->typeConverter->convertTo($input, $wantedType); |
| 133 | } catch (Throwable $error) { |
| 134 | throw new CouldNotCastPropertyException( |
| 135 | $this->storageProperty ?? null, |
| 136 | $this->domainObject, |
| 137 | $wantedType, |
| 138 | $error |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param array<string, mixed> $altered |
| 145 | */ |
| 146 | private function clone(array $altered): self |
| 147 | { |
| 148 | $properties = get_object_vars($this) + $altered; |
| 149 | if (!isset($properties['parentContext'])) { |
| 150 | $properties['parentContext'] = $this; |
| 151 | } |
| 152 | $clone = (new ReflectionClass($this))->newInstanceWithoutConstructor(); |
| 153 | foreach ($properties as $propertyName => $propertyValue) { |
| 154 | $clone->$propertyName = $propertyValue; |
| 155 | } |
| 156 | |
| 157 | return $clone; |
| 158 | } |
| 159 | } |