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
|
|
|
return $this->schema; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Schema data |
85
|
|
|
* @param mixed $schema object schema |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function setSchema($schema) |
89
|
|
|
{ |
90
|
|
|
$this->schema = $schema; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Type of response data |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getType() |
98
|
|
|
{ |
99
|
|
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Type for representation |
104
|
|
|
* @param mixed $type string view |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setType($type) |
108
|
|
|
{ |
109
|
|
|
$this->type = $type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Time for this route data to be cached |
114
|
|
|
* @return mixed |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function getCacheTime() |
117
|
|
|
{ |
118
|
|
|
return $this->cacheTime; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Time for this route data to be cached |
123
|
|
|
* @param integer $cacheTime seconds to be cached |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setCacheTime($cacheTime) |
127
|
|
|
{ |
128
|
|
|
$this->cacheTime = (int) $cacheTime; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Build a output Db Model aggregation pipeline array. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getAggregate($params = []) |
137
|
|
|
{ |
138
|
|
|
$aggregate = $this->getParameterizedAggregate($params); |
139
|
|
|
|
140
|
|
|
/* |
141
|
|
|
$pipeline = []; |
142
|
|
|
|
143
|
|
|
foreach ($aggregate as $op => $query) { |
144
|
|
|
$pipeline[] = [ |
145
|
|
|
$op => $this->parseObjectDates($query) |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
*/ |
149
|
|
|
|
150
|
|
|
if (empty($aggregate)) { |
151
|
|
|
throw new InvalidArgumentException('Wrong configuration for Aggregation pipeline'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $aggregate; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* get Params |
159
|
|
|
* |
160
|
|
|
* @return mixed Params |
161
|
|
|
*/ |
162
|
|
|
public function getParams() |
163
|
|
|
{ |
164
|
|
|
return $this->params; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* set Params |
169
|
|
|
* |
170
|
|
|
* @param mixed $params params |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function setParams($params) |
175
|
|
|
{ |
176
|
|
|
$this->params = $params; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Enabling to possibility to create dtae queries |
181
|
|
|
* Will replace PARSE_DATE(date|format) |
182
|
|
|
* sample: PARSE_DATE(-4 years|Y) -> new DateTime(-4 years)->format(Y) -> 2013 |
183
|
|
|
* |
184
|
|
|
* @param object $query Aggregation query |
185
|
|
|
* @return object |
186
|
|
|
*/ |
187
|
|
|
private function parseObjectDates($query) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$string = json_encode($query); |
190
|
|
|
preg_match_all('/PARSE_DATE\(([^\)]+)\)/', $string, $matches); |
191
|
|
|
if ($matches && array_key_exists(1, $matches) && is_array($matches[1])) { |
192
|
|
|
foreach ($matches[0] as $key => $value) { |
193
|
|
|
$formatting = explode('|', $matches[1][$key]); |
194
|
|
|
$date = new \DateTime($formatting[0]); |
195
|
|
|
$string = str_replace('"'.$value.'"', $date->format($formatting[1]), $string); |
196
|
|
|
} |
197
|
|
|
$query = json_decode($string); |
198
|
|
|
} |
199
|
|
|
return $query; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function getParameterizedAggregate($params) |
203
|
|
|
{ |
204
|
|
|
$encoded = json_encode($this->aggregate); |
205
|
|
|
|
206
|
|
|
// are there any params? |
207
|
|
|
if (is_array($params) && !empty($params)) { |
208
|
|
|
foreach ($params as $name => $value) { |
209
|
|
|
// replace single standalone values in json |
210
|
|
|
if (is_int($value) || is_bool($value)) { |
211
|
|
|
$encoded = preg_replace('/"\$\{'.$name.'\}"/', $value, $encoded); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// the balance |
215
|
|
|
$encoded = preg_replace('/\$\{'.$name.'\}/', $value, $encoded); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
//var_dump($encoded); die; |
220
|
|
|
|
221
|
|
|
return json_decode($encoded, true); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
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.