Total Complexity | 13 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | abstract class PageData extends DataTransferObject implements Arrayable |
||
10 | { |
||
11 | 9 | public static function make(array $data): self |
|
14 | } |
||
15 | |||
16 | // https://github.com/spatie/data-transfer-object/issues/64 |
||
17 | 5 | protected function parseArray(array $array): array |
|
18 | { |
||
19 | 5 | foreach ($array as $key => $value) { |
|
20 | 5 | if ($this->isDateTime($value)) { |
|
21 | 1 | $array[$key] = $value->format(DATE_RFC3339); |
|
22 | |||
23 | 1 | continue; |
|
24 | } |
||
25 | |||
26 | 5 | if ($this->isArrayable($value)) { |
|
27 | 1 | $array[$key] = $value->toArray(); |
|
28 | |||
29 | 1 | continue; |
|
30 | } |
||
31 | |||
32 | 5 | if ($this->isStringable($value)) { |
|
33 | 5 | $array[$key] = $value->__toString(); |
|
34 | |||
35 | 5 | continue; |
|
36 | } |
||
37 | |||
38 | 5 | if (is_array($value)) { |
|
39 | 5 | $array[$key] = $this->parseArray($value); |
|
40 | } |
||
41 | } |
||
42 | |||
43 | 5 | return $array; |
|
44 | } |
||
45 | |||
46 | 5 | protected function isDateTime($value): bool |
|
47 | { |
||
48 | 5 | return $value instanceof DateTime; |
|
49 | } |
||
50 | |||
51 | 5 | protected function isArrayable($value): bool |
|
52 | { |
||
53 | 5 | return $this->isObjectWithMethod($value, 'toArray'); |
|
54 | } |
||
55 | |||
56 | 5 | protected function isStringable($value): bool |
|
59 | } |
||
60 | |||
61 | 5 | protected function isObjectWithMethod($value, string $method): bool |
|
62 | { |
||
66 |