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 http://opensource.org/licenses/gpl-license.php GNU Public License |
14
|
|
|
* @link http://swisscom.ch |
15
|
|
|
*/ |
16
|
|
|
class AnalyticModel |
17
|
|
|
{ |
18
|
|
|
protected $collection; |
19
|
|
|
protected $route; |
20
|
|
|
protected $aggregate; |
21
|
|
|
protected $pipeline; |
22
|
|
|
protected $schema; |
23
|
|
|
protected $type; |
24
|
|
|
protected $cacheTime; |
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
|
|
|
* Mongodb Aggregates |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getAggregate() |
69
|
|
|
{ |
70
|
|
|
return $this->aggregate ?: []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set mongodb query |
75
|
|
|
* @param mixed $aggregate object type for query data |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function setAggregate($aggregate) |
79
|
|
|
{ |
80
|
|
|
$this->aggregate = $aggregate; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $pipeline Data array for query |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function setPipeline($pipeline) |
88
|
|
|
{ |
89
|
|
|
$this->pipeline = $pipeline; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Schema for response |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getSchema() |
97
|
|
|
{ |
98
|
|
|
return $this->schema; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Schema data |
103
|
|
|
* @param mixed $schema object schema |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function setSchema($schema) |
107
|
|
|
{ |
108
|
|
|
$this->schema = $schema; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Type of response data |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
public function getType() |
116
|
|
|
{ |
117
|
|
|
return $this->type; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Type for representation |
122
|
|
|
* @param mixed $type string view |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function setType($type) |
126
|
|
|
{ |
127
|
|
|
$this->type = $type; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Time for this route data to be cached |
132
|
|
|
* @return mixed |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
public function getCacheTime() |
135
|
|
|
{ |
136
|
|
|
return $this->cacheTime; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Time for this route data to be cached |
141
|
|
|
* @param integer $cacheTime seconds to be cached |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function setCacheTime($cacheTime) |
145
|
|
|
{ |
146
|
|
|
$this->cacheTime = (int) $cacheTime; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Build a output Db Model aggregation pipeline array. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getPipeline() |
157
|
|
|
{ |
158
|
|
|
$rtnPipeline = []; |
159
|
|
|
|
160
|
|
|
if ($pipeline = $this->pipeline) { |
161
|
|
|
foreach ($pipeline as $pipe) { |
162
|
|
|
foreach ($pipe as $op => $query) { |
163
|
|
|
$rtnPipeline[] = [ |
164
|
|
|
$op => $this->parseObjectDates($query) |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} elseif ($aggregate = $this->getAggregate()) { |
169
|
|
|
foreach ($aggregate as $op => $query) { |
170
|
|
|
$rtnPipeline[] = [ |
171
|
|
|
$op => $this->parseObjectDates($query) |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (empty($rtnPipeline)) { |
177
|
|
|
throw new InvalidArgumentException('Wrong configuration for Aggregation pipeline'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $rtnPipeline; |
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
|
|
|
|
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.