Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
31 / 31 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
CompositeValueObject | |
100.00% |
31 / 31 |
|
100.00% |
3 / 3 |
14 | |
100.00% |
1 / 1 |
getFields | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
fromNative | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
toNative | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | namespace Apie\Core\ValueObjects; |
3 | |
4 | use Apie\Core\Attributes\Internal; |
5 | use Apie\Core\ValueObjects\Fields\FieldInterface; |
6 | use Apie\Core\ValueObjects\Fields\FromProperty; |
7 | use Apie\Serializer\Exceptions\ValidationException; |
8 | use Exception; |
9 | use ReflectionClass; |
10 | |
11 | /** |
12 | * Use this trait to make a value object consisting of multiple properties. |
13 | */ |
14 | trait CompositeValueObject |
15 | { |
16 | /** |
17 | * @var array<string, FieldInterface> |
18 | */ |
19 | private static array $fields; |
20 | |
21 | /** |
22 | * @return array<string, FieldInterface> |
23 | */ |
24 | public static function getFields(): array |
25 | { |
26 | if (!isset(self::$fields)) { |
27 | $fields = []; |
28 | $refl = new ReflectionClass(__CLASS__); |
29 | foreach ($refl->getProperties() as $property) { |
30 | if ($property->isStatic()) { |
31 | continue; |
32 | } |
33 | if (!empty($property->getAttributes(Internal::class))) { |
34 | continue; |
35 | } |
36 | $fields[$property->getName()] = new FromProperty($property); |
37 | } |
38 | self::$fields = $fields; |
39 | } |
40 | |
41 | return self::$fields; |
42 | } |
43 | |
44 | public static function fromNative(mixed $input): self |
45 | { |
46 | $input = Utils::toArray($input); |
47 | $refl = new ReflectionClass(__CLASS__); |
48 | $instance = $refl->newInstanceWithoutConstructor(); |
49 | $errors = []; |
50 | foreach (self::getFields() as $fieldName => $field) { |
51 | try { |
52 | if (array_key_exists($fieldName, $input)) { |
53 | $field->fromNative($instance, $input[$fieldName]); |
54 | } else { |
55 | $field->fillMissingField($instance); |
56 | } |
57 | } catch (Exception $error) { |
58 | $errors[$fieldName] = $error; |
59 | } |
60 | } |
61 | if (!empty($errors)) { |
62 | throw ValidationException::createFromArray($errors); |
63 | } |
64 | if (is_callable([$instance, 'validateState'])) { |
65 | $instance->validateState(); |
66 | } |
67 | return $instance; |
68 | } |
69 | |
70 | /** |
71 | * @return array<string, mixed> |
72 | */ |
73 | public function toNative(): array |
74 | { |
75 | $result = []; |
76 | foreach (self::getFields() as $fieldName => $field) { |
77 | if ($field->isInitialized($this)) { |
78 | $result[$fieldName] = $field->toNative($this); |
79 | } |
80 | } |
81 | return $result; |
82 | } |
83 | } |