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 Transducer |
13
|
|
|
* @package Bricks\Data |
14
|
|
|
*/ |
15
|
|
|
class Transducer implements TransducerInterface |
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->process($data, $this->config); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $data |
50
|
|
|
* @param array $config |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
18 |
|
private function process(array $data, array $config): array |
54
|
|
|
{ |
55
|
18 |
|
$processed = []; |
56
|
18 |
|
foreach ($data as $field => $value) { |
57
|
18 |
|
if (is_array($value) && !empty($value) && ArrHelper::isStrKeysArray($value)) { |
58
|
1 |
|
$processed[$field] = $this->process($value, $config[$field]); |
59
|
|
|
} else { |
60
|
18 |
|
$method = $config[$field] ?? null; |
61
|
18 |
|
if (empty($method) || $value === null) { |
62
|
2 |
|
$processed[$field] = $value; |
63
|
|
|
} else { |
64
|
18 |
|
$this->assertConversionMethodExists($method); |
65
|
17 |
|
$processed[$field] = call_user_func([$this, $method], $value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
16 |
|
return $processed; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|int $value |
74
|
|
|
* @return Carbon |
75
|
|
|
*/ |
76
|
2 |
|
protected function dateTime($value): Carbon |
77
|
|
|
{ |
78
|
2 |
|
if (is_string($value)) { |
79
|
1 |
|
return Carbon::parse($value); |
80
|
2 |
|
} elseif (is_int($value)) { |
81
|
1 |
|
return Carbon::now()->setTimestamp($value); |
82
|
|
|
} |
83
|
1 |
|
throw new InvalidRequestException('unexpected datetime format'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $value |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function intArray(array $value): array |
91
|
|
|
{ |
92
|
6 |
|
$array = array_map(function ($val) { |
93
|
5 |
|
return (int)$val; |
94
|
6 |
|
}, array_filter($value)); |
95
|
|
|
|
96
|
6 |
|
return array_values($array); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $value |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function floatArray(array $value): array |
104
|
|
|
{ |
105
|
1 |
|
$array = array_map(function ($val) { |
106
|
1 |
|
return (float)$val; |
107
|
1 |
|
}, array_filter($value)); |
108
|
|
|
|
109
|
1 |
|
return array_values($array); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $value |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function strArray(array $value): array |
117
|
|
|
{ |
118
|
1 |
|
$array = array_map(function ($val) { |
119
|
1 |
|
return (string)$val; |
120
|
1 |
|
}, array_filter($value)); |
121
|
|
|
|
122
|
1 |
|
return array_values($array); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
7 |
|
protected function integer($value): int |
130
|
|
|
{ |
131
|
7 |
|
return (int)$value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param mixed $value |
136
|
|
|
* @return float |
137
|
|
|
*/ |
138
|
1 |
|
protected function float($value): float |
139
|
|
|
{ |
140
|
1 |
|
return (float)$value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param mixed $value |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
7 |
|
protected function string($value): string |
148
|
|
|
{ |
149
|
7 |
|
return '' . $value; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param mixed $value |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
1 |
|
protected function array($value): array |
157
|
|
|
{ |
158
|
1 |
|
return (array)$value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $value |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
2 |
|
protected function boolean($value): bool |
166
|
|
|
{ |
167
|
2 |
|
return is_string($value) ? $value === 'true' : (bool)$value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $method |
172
|
|
|
* |
173
|
|
|
* @throws ConfigurationException |
174
|
|
|
*/ |
175
|
18 |
|
private function assertConversionMethodExists(string $method) |
176
|
|
|
{ |
177
|
18 |
|
if (!method_exists($this, $method)) { |
178
|
1 |
|
throw new ConfigurationException('unknown field type: ' . $method); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |