Total Complexity | 18 |
Total Lines | 84 |
Duplicated Lines | 0 % |
Coverage | 91.67% |
Changes | 0 |
1 | <?php |
||
8 | class TypeCaster |
||
9 | { |
||
10 | /** |
||
11 | * @var PropertyType |
||
12 | */ |
||
13 | protected $type; |
||
14 | |||
15 | /** |
||
16 | * TypeCaster constructor. |
||
17 | * @param PropertyType $type |
||
18 | */ |
||
19 | 41 | public function __construct(PropertyType $type) |
|
20 | { |
||
21 | 41 | $this->type = $type; |
|
22 | 41 | } |
|
23 | |||
24 | 41 | public function cast($value) |
|
33 | } |
||
34 | |||
35 | 41 | protected function castDto($value) |
|
36 | { |
||
37 | 41 | foreach ($this->type->getTypes() as $type) { |
|
38 | 40 | if (is_subclass_of($type, DataTransferObject::class)) { |
|
39 | 6 | if (is_array($value)) { |
|
40 | 40 | return new $type($value); |
|
41 | } |
||
42 | } |
||
43 | } |
||
44 | |||
45 | 40 | return $value; |
|
46 | } |
||
47 | |||
48 | 2 | protected function castCollection(array $values) |
|
49 | { |
||
50 | 2 | $castTo = null; |
|
51 | |||
52 | 2 | foreach ($this->type->getArrayTypes() as $type) { |
|
53 | 2 | if (! is_subclass_of($type, DataTransferObject::class)) { |
|
54 | continue; |
||
55 | } |
||
56 | |||
57 | 2 | $castTo = $type; |
|
58 | |||
59 | 2 | break; |
|
60 | } |
||
61 | |||
62 | 2 | if (! $castTo) { |
|
63 | return $values; |
||
64 | } |
||
65 | |||
66 | 2 | $casts = []; |
|
67 | |||
68 | 2 | foreach ($values as $value) { |
|
69 | 2 | $casts[] = new $castTo($value); |
|
70 | } |
||
71 | |||
72 | 2 | return $casts; |
|
73 | } |
||
74 | |||
75 | 7 | protected function shouldBeCastToCollection(array $values): bool |
|
92 | } |
||
93 | } |
||
94 |