1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Component\ODMElasticsearch\Transformer\Visitor; |
6
|
|
|
|
7
|
|
|
use CCT\Component\ODMElasticsearch\Transformer\Exception\TransformationFailedException; |
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
class ReverseElasticsearchVisitor extends AbstractReverseVisitor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Allows visitors to convert the input data to a different representation |
14
|
|
|
* before the actual formatting process starts. |
15
|
|
|
* |
16
|
|
|
* @param mixed $data |
17
|
|
|
* |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function prepare($data) |
21
|
|
|
{ |
22
|
|
|
return $data; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param mixed $data |
27
|
|
|
* @param array $config |
28
|
|
|
* |
29
|
|
|
* @return mixed|null |
30
|
|
|
*/ |
31
|
|
|
public function visitNull($data, array $config) |
32
|
|
|
{ |
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $data |
38
|
|
|
* @param array $config |
39
|
|
|
* |
40
|
|
|
* @return mixed|string |
41
|
|
|
*/ |
42
|
1 |
|
public function visitString($data, array $config) |
43
|
|
|
{ |
44
|
1 |
|
return (string)$data; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $data |
49
|
|
|
* @param array $config |
50
|
|
|
* |
51
|
|
|
* @return bool|mixed |
52
|
|
|
*/ |
53
|
|
|
public function visitBoolean($data, array $config) |
54
|
|
|
{ |
55
|
|
|
return (bool)$data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function visitInteger($data, array $config) |
59
|
|
|
{ |
60
|
|
|
return (int)$data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function visitDouble($data, array $config) |
64
|
|
|
{ |
65
|
|
|
return (float)$data; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function visitDate($data, array $config) |
69
|
|
|
{ |
70
|
|
|
if (null === $data) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->convertToDataTime($data); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $data |
79
|
|
|
* @param array $config |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
5 |
|
public function visitDateTime($data, array $config) |
84
|
|
|
{ |
85
|
5 |
|
if (null === $data) { |
86
|
1 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
return $this->convertToDataTime($data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param mixed $data |
94
|
|
|
* @param array $config |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function visitTime($data, array $config) |
99
|
|
|
{ |
100
|
|
|
$inSeconds = (int)$data; |
101
|
|
|
|
102
|
|
|
$hours = floor($inSeconds / 3600); |
103
|
|
|
$minutes = floor($inSeconds / 60 % 60); |
104
|
|
|
$seconds = floor($inSeconds % 60); |
105
|
|
|
|
106
|
|
|
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $data |
111
|
|
|
* @param array $config |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
1 |
|
public function visitArray(array $data, array $config) |
116
|
|
|
{ |
117
|
1 |
|
$visitedArray = []; |
118
|
1 |
|
foreach ($data as $index => $item) { |
119
|
|
|
//Navigate over items again |
120
|
1 |
|
$itemConfig = null; |
121
|
1 |
|
if (isset($config['class'])) { |
122
|
1 |
|
$itemConfig = ['type' => 'object', 'params' => [], 'class' => $config['class']]; |
123
|
|
|
} |
124
|
1 |
|
$visitedArray[$index] = $this->dataNavigator->navigate($item, $this, $itemConfig); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $visitedArray; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function visitObject($data, array $config) |
131
|
|
|
{ |
132
|
1 |
|
return $this->navigateObjectHydrate($data, $config); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string|integer $date |
137
|
|
|
* |
138
|
|
|
* @return \DateTimeInterface |
139
|
|
|
*/ |
140
|
4 |
|
private function convertToDataTime($date): ?\DateTimeInterface |
141
|
|
|
{ |
142
|
|
|
try { |
143
|
4 |
|
if (is_int($date)) { |
144
|
1 |
|
$normalizedDate = $this->normalizeDateFormat((string)$date); |
145
|
1 |
|
$convertedDate = DateTime::createFromFormat( |
146
|
1 |
|
'U.u', |
147
|
1 |
|
$normalizedDate |
148
|
|
|
); |
149
|
|
|
|
150
|
1 |
|
if (false === $convertedDate) { |
151
|
|
|
throw new TransformationFailedException( |
152
|
|
|
sprintf('Date "%s" could not be converted to a DateTime object', (string)$date) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $convertedDate; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
return new DateTime($date); |
160
|
3 |
|
} catch (\Exception $exception) { |
161
|
3 |
|
throw new TransformationFailedException( |
162
|
3 |
|
sprintf('Date "%s" could not be converted to a DateTime object', (string)$date), |
163
|
3 |
|
0, |
164
|
3 |
|
$exception |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $data |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
1 |
|
protected function normalizeDateFormat(string $data): string |
175
|
|
|
{ |
176
|
1 |
|
return sprintf( |
177
|
1 |
|
'%s.%s', |
178
|
1 |
|
substr($data, 0, 10), |
179
|
1 |
|
round((int) substr($data, -3, 3)) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|