1 | <?php |
||
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() |
||
34 | |||
35 | /** |
||
36 | * Set value of collection |
||
37 | * @param mixed $collection string name |
||
38 | * @return void |
||
39 | */ |
||
40 | public function setCollection($collection) |
||
44 | |||
45 | /** |
||
46 | * Route path |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function getRoute() |
||
53 | |||
54 | /** |
||
55 | * Set path |
||
56 | * @param mixed $route string route |
||
57 | * @return void |
||
58 | */ |
||
59 | public function setRoute($route) |
||
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) |
||
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); |
||
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.