Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.30% |
26 / 27 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
NormalizedData | |
96.30% |
26 / 27 |
|
80.00% |
4 / 5 |
12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validationCheck | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
createNewObject | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
callSetters | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
modifyExistingObject | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Serializer\Context; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\Exceptions\IndexNotFoundException; |
6 | use Apie\Serializer\Exceptions\ValidationException; |
7 | use Exception; |
8 | use ReflectionClass; |
9 | |
10 | /** |
11 | * @template T of object |
12 | */ |
13 | class NormalizedData |
14 | { |
15 | /** |
16 | * @param ReflectionClass<T> $class |
17 | * @param array<string, NormalizedValue> $normalizedValues |
18 | * @param array<string, Exception> $validationErrors |
19 | */ |
20 | public function __construct( |
21 | private readonly ReflectionClass $class, |
22 | private readonly ApieContext $apieContext, |
23 | private readonly array $normalizedValues, |
24 | private readonly array $validationErrors |
25 | ) { |
26 | } |
27 | |
28 | private function validationCheck(): void |
29 | { |
30 | if (!empty($this->validationErrors)) { |
31 | throw ValidationException::createFromArray($this->validationErrors); |
32 | } |
33 | } |
34 | |
35 | /** |
36 | * @return T |
37 | */ |
38 | public function createNewObject(): object |
39 | { |
40 | $this->validationCheck(); |
41 | $constructor = $this->class->getConstructor(); |
42 | $constructorArguments = []; |
43 | if ($constructor) { |
44 | foreach ($constructor->getParameters() as $parameter) { |
45 | if (isset($this->normalizedValues[$parameter->name])) { |
46 | $constructorArguments[] = $this->normalizedValues[$parameter->name]->getNormalizedValue(); |
47 | } else { |
48 | if (!$parameter->isDefaultValueAvailable()) { |
49 | throw new IndexNotFoundException($parameter->name); |
50 | } |
51 | $constructorArguments[] = $parameter->getDefaultValue(); |
52 | } |
53 | } |
54 | } |
55 | $object = $this->class->newInstanceArgs($constructorArguments); |
56 | |
57 | return $this->callSetters($object); |
58 | } |
59 | |
60 | /** |
61 | * @param T $object |
62 | * @return T |
63 | */ |
64 | private function callSetters(object $object): object |
65 | { |
66 | foreach ($this->normalizedValues as $normalizedValue) { |
67 | $value = $normalizedValue->getNormalizedValue(); |
68 | $fieldMetadata = $normalizedValue->getFieldMetadata(); |
69 | if ($fieldMetadata->isField()) { |
70 | $fieldMetadata->setValue( |
71 | $object, |
72 | $value, |
73 | $this->apieContext |
74 | ); |
75 | } |
76 | } |
77 | return $object; |
78 | } |
79 | |
80 | /** |
81 | * @param T $object |
82 | * @return T |
83 | */ |
84 | public function modifyExistingObject(object $object): object |
85 | { |
86 | $this->validationCheck(); |
87 | |
88 | return $this->callSetters($object); |
89 | } |
90 | } |