1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bricks\Data; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Bricks\Exception\ConfigurationException; |
7
|
|
|
use Bricks\Exception\InvalidRequestException; |
8
|
|
|
use Bricks\Helper\ArrHelper; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Cast |
13
|
|
|
* @package Bricks\Data |
14
|
|
|
*/ |
15
|
|
|
class Cast implements CastInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $config |
24
|
|
|
*/ |
25
|
19 |
|
private function __construct(array $config) |
26
|
|
|
{ |
27
|
19 |
|
$this->config = ArrHelper::unflatten($config); |
28
|
19 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $config |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
19 |
|
public static function create(array $config) |
35
|
|
|
{ |
36
|
19 |
|
return new static($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $data |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
18 |
|
public function execute(array $data): array |
44
|
|
|
{ |
45
|
18 |
|
return $this->cast($data, $this->config); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $data |
50
|
|
|
* @param array $config |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
18 |
|
private function cast($data, array $config) |
54
|
|
|
{ |
55
|
18 |
|
$casted = []; |
56
|
18 |
|
foreach ($data as $field => $value) { |
57
|
18 |
|
if (is_array($value) && !empty($value) && ArrHelper::isStrKeysArray($value)) { |
58
|
1 |
|
$casted[$field] = $this->cast($value, $config[$field]); |
59
|
|
|
} else { |
60
|
18 |
|
$method = $config[$field] ?? null; |
61
|
18 |
|
if (empty($method) || $value === null) { |
62
|
2 |
|
$casted[$field] = $value; |
63
|
|
|
} else { |
64
|
18 |
|
if (!method_exists($this, $method)) { |
65
|
1 |
|
throw new ConfigurationException('unknown field type: ' . $method); |
66
|
|
|
} |
67
|
17 |
|
$casted[$field] = call_user_func([$this, $method], $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
16 |
|
return $casted; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|int $value |
76
|
|
|
* @return Carbon |
77
|
|
|
*/ |
78
|
2 |
|
private function dateTime($value): Carbon |
79
|
|
|
{ |
80
|
2 |
|
if (is_string($value)) { |
81
|
1 |
|
return Carbon::parse($value); |
82
|
2 |
|
} elseif (is_int($value)) { |
83
|
1 |
|
return Carbon::now()->setTimestamp($value); |
84
|
|
|
} |
85
|
1 |
|
throw new InvalidRequestException('unexpected datetime format'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $value |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
private function intArray(array $value): array |
|
|
|
|
93
|
|
|
{ |
94
|
6 |
|
$array = array_map(function ($val) { |
95
|
5 |
|
return $this->integer($val); |
96
|
6 |
|
}, array_filter($value)); |
97
|
|
|
|
98
|
6 |
|
return array_values($array); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $value |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
private function floatArray(array $value): array |
|
|
|
|
106
|
|
|
{ |
107
|
1 |
|
$array = array_map(function ($val) { |
108
|
1 |
|
return $this->float($val); |
109
|
1 |
|
}, array_filter($value)); |
110
|
|
|
|
111
|
1 |
|
return array_values($array); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $value |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
private function strArray(array $value): array |
|
|
|
|
119
|
|
|
{ |
120
|
1 |
|
$array = array_map(function ($val) { |
121
|
1 |
|
return $this->string($val); |
122
|
1 |
|
}, array_filter($value)); |
123
|
|
|
|
124
|
1 |
|
return array_values($array); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $value |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
8 |
|
private function integer($value): int |
132
|
|
|
{ |
133
|
8 |
|
return (int)$value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @return float |
139
|
|
|
*/ |
140
|
2 |
|
private function float($value): float |
141
|
|
|
{ |
142
|
2 |
|
return (float)$value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $value |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
8 |
|
private function string($value): string |
150
|
|
|
{ |
151
|
8 |
|
return '' . $value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $value |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
1 |
|
private function array($value): array |
159
|
|
|
{ |
160
|
1 |
|
return (array)$value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param mixed $value |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
2 |
|
private function boolean($value): bool |
168
|
|
|
{ |
169
|
2 |
|
return is_string($value) ? $value === 'true' : (bool)$value; |
170
|
|
|
} |
171
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.