Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.19% |
23 / 27 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| FromProperty | |
85.19% |
23 / 27 |
|
88.89% |
8 / 9 |
20.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getTypehint | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| isOptional | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| fromNative | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| fillField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fillMissingField | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| isInitialized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toNative | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\ValueObjects\Fields; |
| 3 | |
| 4 | use Apie\Core\Attributes\Optional; |
| 5 | use Apie\Core\Exceptions\InvalidTypeException; |
| 6 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 7 | use Apie\Core\ValueObjects\Utils; |
| 8 | use ReflectionIntersectionType; |
| 9 | use ReflectionNamedType; |
| 10 | use ReflectionProperty; |
| 11 | use ReflectionUnionType; |
| 12 | use UnitEnum; |
| 13 | |
| 14 | /** |
| 15 | * FieldInterface implementation that reads a property from an object with reflection. |
| 16 | */ |
| 17 | final class FromProperty implements FieldInterface |
| 18 | { |
| 19 | private ReflectionProperty $property; |
| 20 | |
| 21 | public function __construct(ReflectionProperty $property) |
| 22 | { |
| 23 | $this->property = $property; |
| 24 | $property->setAccessible(true); |
| 25 | } |
| 26 | |
| 27 | public function getTypehint(): string |
| 28 | { |
| 29 | $type = $this->property->getType(); |
| 30 | if ($type instanceof ReflectionNamedType) { |
| 31 | return ($type->allowsNull() ? $type->getName() : ($type->getName() . '|null')); |
| 32 | } |
| 33 | return (string) $type; |
| 34 | } |
| 35 | |
| 36 | public function isOptional(): bool |
| 37 | { |
| 38 | return $this->property->hasDefaultValue() |
| 39 | || !empty($this->property->getAttributes(Optional::class)) |
| 40 | || $this->property->getType()->allowsNull(); |
| 41 | } |
| 42 | |
| 43 | public function fromNative(ValueObjectInterface $instance, mixed $value): void |
| 44 | { |
| 45 | $type = $this->property->getType(); |
| 46 | if ($type instanceof ReflectionUnionType || $type instanceof ReflectionNamedType) { |
| 47 | self::fillField($instance, Utils::toTypehint($type, $value)); |
| 48 | return; |
| 49 | } |
| 50 | throw new InvalidTypeException($type, 'ReflectionUnionType|ReflectionNamedType'); |
| 51 | } |
| 52 | |
| 53 | public function fillField(ValueObjectInterface $instance, mixed $value): void |
| 54 | { |
| 55 | $this->property->setValue($instance, $value); |
| 56 | } |
| 57 | |
| 58 | public function fillMissingField(ValueObjectInterface $instance): void |
| 59 | { |
| 60 | if (!$this->isOptional()) { |
| 61 | $type = $this->property->getType(); |
| 62 | if (null === $type || $type instanceof ReflectionIntersectionType) { |
| 63 | throw new InvalidTypeException($type, 'ReflectionUnionType|ReflectionNamedType'); |
| 64 | } |
| 65 | throw new InvalidTypeException('(missing value)', (string) $type); |
| 66 | } |
| 67 | if (!empty($this->property->getAttributes(Optional::class))) { |
| 68 | return; |
| 69 | } |
| 70 | $this->property->setValue($instance, $this->property->getDefaultValue()); |
| 71 | } |
| 72 | |
| 73 | public function isInitialized(ValueObjectInterface $instance): bool |
| 74 | { |
| 75 | return $this->property->isInitialized($instance); |
| 76 | } |
| 77 | |
| 78 | public function getValue(ValueObjectInterface $instance): mixed |
| 79 | { |
| 80 | return $this->property->getValue($instance); |
| 81 | } |
| 82 | |
| 83 | public function toNative(ValueObjectInterface $instance): null|array|string|int|float|bool|UnitEnum |
| 84 | { |
| 85 | $value = $this->getValue($instance); |
| 86 | return Utils::toNative($value); |
| 87 | } |
| 88 | } |