1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Schema Class for output data. |
4
|
|
|
*/ |
5
|
|
|
namespace Graviton\AnalyticsBundle\Model; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schema |
11
|
|
|
* |
12
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
13
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
14
|
|
|
* @link http://swisscom.ch |
15
|
|
|
*/ |
16
|
|
|
class AnalyticModel |
17
|
|
|
{ |
18
|
|
|
protected $collection; |
19
|
|
|
protected $route; |
20
|
|
|
protected $aggregate = []; |
21
|
|
|
protected $schema; |
22
|
|
|
protected $type; |
23
|
|
|
protected $cacheTime; |
24
|
|
|
protected $params = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* String collection |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function getCollection() |
31
|
|
|
{ |
32
|
|
|
return $this->collection; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set value of collection |
37
|
|
|
* @param mixed $collection string name |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function setCollection($collection) |
41
|
|
|
{ |
42
|
|
|
$this->collection = $collection; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Route path |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function getRoute() |
50
|
|
|
{ |
51
|
|
|
return $this->route; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set path |
56
|
|
|
* @param mixed $route string route |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function setRoute($route) |
60
|
|
|
{ |
61
|
|
|
$this->route = $route; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set mongodb query |
66
|
|
|
* @param mixed $aggregate object type for query data |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function setAggregate($aggregate) |
70
|
|
|
{ |
71
|
|
|
$this->aggregate = $aggregate; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Schema for response |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function getSchema() |
79
|
|
|
{ |
80
|
|
|
$schema = $this->schema; |
81
|
|
|
$schema->{'x-params'} = $this->getParams(); |
82
|
|
|
return $schema; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Schema data |
87
|
|
|
* @param mixed $schema object schema |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function setSchema($schema) |
91
|
|
|
{ |
92
|
|
|
$this->schema = $schema; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Type of response data |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function getType() |
100
|
|
|
{ |
101
|
|
|
return $this->type; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Type (array or object) |
106
|
|
|
* @param mixed $type string view |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function setType($type) |
110
|
|
|
{ |
111
|
|
|
$this->type = $type; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Time for this route data to be cached |
116
|
|
|
* @return mixed |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
public function getCacheTime() |
119
|
|
|
{ |
120
|
|
|
return $this->cacheTime; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Time for this route data to be cached |
125
|
|
|
* @param integer $cacheTime seconds to be cached |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function setCacheTime($cacheTime) |
129
|
|
|
{ |
130
|
|
|
$this->cacheTime = (int) $cacheTime; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Build a output Db Model aggregation pipeline array. |
135
|
|
|
* |
136
|
|
|
* @param array $params params |
137
|
|
|
* |
138
|
|
|
* @return array the pipeline |
139
|
|
|
*/ |
140
|
|
|
public function getAggregate($params = []) |
141
|
|
|
{ |
142
|
|
|
$aggregate = $this->getParameterizedAggregate($params); |
143
|
|
|
|
144
|
|
|
/* |
145
|
|
|
$pipeline = []; |
146
|
|
|
|
147
|
|
|
foreach ($aggregate as $op => $query) { |
148
|
|
|
$pipeline[] = [ |
149
|
|
|
$op => $this->parseObjectDates($query) |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
*/ |
153
|
|
|
|
154
|
|
|
if (empty($aggregate)) { |
155
|
|
|
throw new InvalidArgumentException('Wrong configuration for Aggregation pipeline'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $aggregate; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* get Params |
163
|
|
|
* |
164
|
|
|
* @return mixed Params |
165
|
|
|
*/ |
166
|
|
|
public function getParams() |
167
|
|
|
{ |
168
|
|
|
return $this->params; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* set Params |
173
|
|
|
* |
174
|
|
|
* @param mixed $params params |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function setParams($params) |
179
|
|
|
{ |
180
|
|
|
$this->params = $params; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Enabling to possibility to create dtae queries |
185
|
|
|
* Will replace PARSE_DATE(date|format) |
186
|
|
|
* sample: PARSE_DATE(-4 years|Y) -> new DateTime(-4 years)->format(Y) -> 2013 |
187
|
|
|
* |
188
|
|
|
* @param object $query Aggregation query |
189
|
|
|
* @return object |
190
|
|
|
*/ |
191
|
|
|
private function parseObjectDates($query) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$string = json_encode($query); |
194
|
|
|
preg_match_all('/PARSE_DATE\(([^\)]+)\)/', $string, $matches); |
195
|
|
|
if ($matches && array_key_exists(1, $matches) && is_array($matches[1])) { |
196
|
|
|
foreach ($matches[0] as $key => $value) { |
197
|
|
|
$formatting = explode('|', $matches[1][$key]); |
198
|
|
|
$date = new \DateTime($formatting[0]); |
199
|
|
|
$string = str_replace('"'.$value.'"', $date->format($formatting[1]), $string); |
200
|
|
|
} |
201
|
|
|
$query = json_decode($string); |
202
|
|
|
} |
203
|
|
|
return $query; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* returns the pipeline with param values replaced |
208
|
|
|
* |
209
|
|
|
* @param array $params the params |
210
|
|
|
* |
211
|
|
|
* @return array the pipeline with values filled in |
212
|
|
|
*/ |
213
|
|
|
private function getParameterizedAggregate(array $params) |
214
|
|
|
{ |
215
|
|
|
$encoded = json_encode($this->aggregate); |
216
|
|
|
|
217
|
|
|
// are there any params? |
218
|
|
|
if (is_array($params) && !empty($params)) { |
219
|
|
|
foreach ($params as $name => $value) { |
220
|
|
|
if (!is_array($value)) { |
221
|
|
|
// replace single standalone values in json |
222
|
|
|
if (is_int($value) || is_bool($value)) { |
223
|
|
|
$encoded = preg_replace('/"\$\{'.$name.'\}"/', $value, $encoded); |
224
|
|
|
} |
225
|
|
|
// the balance |
226
|
|
|
$encoded = preg_replace('/\$\{'.$name.'\}/', $value, $encoded); |
227
|
|
|
} else { |
228
|
|
|
$encoded = preg_replace('/"\$\{'.$name.'\}"/', json_encode($value), $encoded); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return json_decode($encoded, true); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.