1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Component\ODMElasticsearch\Transformer\Visitor; |
6
|
|
|
|
7
|
|
|
class ElasticsearchVisitor extends AbstractVisitor |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Allows visitors to convert the input data to a different representation |
11
|
|
|
* before the actual formatting process starts. |
12
|
|
|
* |
13
|
|
|
* @param mixed $data |
14
|
|
|
* |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
|
|
public function prepare($data) |
18
|
|
|
{ |
19
|
|
|
return $data; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param mixed $data |
24
|
|
|
* @param array $config |
25
|
|
|
* |
26
|
|
|
* @return mixed|null |
27
|
|
|
*/ |
28
|
|
|
public function visitNull($data, array $config) |
29
|
|
|
{ |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $data |
35
|
|
|
* @param array $config |
36
|
|
|
* |
37
|
|
|
* @return mixed|string |
38
|
|
|
*/ |
39
|
2 |
|
public function visitString($data, array $config) |
40
|
|
|
{ |
41
|
2 |
|
return (string)$data; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $data |
46
|
|
|
* @param array $config |
47
|
|
|
* |
48
|
|
|
* @return bool|mixed |
49
|
|
|
*/ |
50
|
2 |
|
public function visitBoolean($data, array $config) |
51
|
|
|
{ |
52
|
2 |
|
return (bool)$data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function visitInteger($data, array $config) |
56
|
|
|
{ |
57
|
|
|
return (int)$data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function visitDouble($data, array $config) |
61
|
|
|
{ |
62
|
|
|
return (float)$data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function visitDate($data, array $config) |
66
|
|
|
{ |
67
|
|
|
$timestamp = 0; |
68
|
|
|
if ($data instanceof \DateTimeInterface) { |
69
|
|
|
$timestamp = (int) substr($data->format('Uu'), 0, 13); |
70
|
|
|
return $timestamp; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (\is_string($data)) { |
74
|
|
|
$timestamp = strtotime($data); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return round($timestamp * 1000); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed $data |
82
|
|
|
* @param array $config |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function visitDateTime($data, array $config) |
87
|
|
|
{ |
88
|
|
|
return $this->visitDate($data, $config); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param mixed $data |
93
|
|
|
* @param array $config |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function visitTime($data, array $config) |
98
|
|
|
{ |
99
|
|
|
if (null === $data) { |
100
|
|
|
return 0; |
101
|
|
|
} |
102
|
|
|
$parsed = date_parse($data); |
103
|
|
|
if (false === $parsed) { |
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $data |
112
|
|
|
* @param array $config |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
2 |
|
public function visitArray(array $data, array $config) |
117
|
|
|
{ |
118
|
2 |
|
$visitedArray = []; |
119
|
2 |
|
foreach ($data as $index => $item) { |
120
|
|
|
//Navigate over items again |
121
|
1 |
|
$itemConfig = null; |
122
|
1 |
|
if (isset($config['type_class'])) { |
123
|
|
|
$itemConfig = ['type' => 'object', 'params' => [], 'type_class', 'type_class']; |
124
|
|
|
} |
125
|
1 |
|
$visitedArray[$index] = $this->dataNavigator->navigate($item, $this, $itemConfig); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
return $visitedArray; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
public function visitObject($data, array $config) |
132
|
|
|
{ |
133
|
2 |
|
return $this->navigateObject($data); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|