1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Algatux\MongoDB\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use MongoDB\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class QueryBuilder. |
9
|
|
|
*/ |
10
|
|
|
class QueryBuilder |
11
|
|
|
{ |
12
|
|
|
/** @var Collection */ |
13
|
|
|
private $collection; |
14
|
|
|
/** @var Expression */ |
15
|
|
|
private $expression; |
16
|
|
|
/** @var array */ |
17
|
|
|
private $options; |
18
|
|
|
/** @var int */ |
19
|
|
|
private $queryType; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* QueryBuilder constructor. |
23
|
|
|
* |
24
|
|
|
* @param Collection $collection |
25
|
|
|
*/ |
26
|
18 |
|
public function __construct(Collection $collection) |
27
|
|
|
{ |
28
|
18 |
|
$this->collection = $collection; |
29
|
18 |
|
$this->queryType = Query::TYPE_FIND; |
30
|
18 |
|
$this->expression = new Expression(); |
31
|
18 |
|
$this->options = []; |
32
|
18 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tells the query builder to execute a find |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
8 |
|
public function find() |
40
|
|
|
{ |
41
|
8 |
|
return $this->setType(Query::TYPE_FIND); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tells the query builder to execute a count |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
1 |
|
public function count() |
50
|
|
|
{ |
51
|
1 |
|
return $this->setType(Query::TYPE_COUNT); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $type |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
9 |
|
protected function setType(int $type) |
60
|
|
|
{ |
61
|
9 |
|
$this->queryType = $type; |
62
|
|
|
|
63
|
9 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds and filter |
68
|
|
|
* |
69
|
|
|
* @param array|Expression $expression |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function and($expression) |
|
|
|
|
74
|
|
|
{ |
75
|
6 |
|
$this->expression->and(...func_get_args()); |
76
|
|
|
|
77
|
6 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds or filter |
82
|
|
|
* |
83
|
|
|
* @param array|Expression $expression |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
2 |
|
public function or($expression) |
|
|
|
|
88
|
|
|
{ |
89
|
2 |
|
$this->expression->or(...func_get_args()); |
90
|
|
|
|
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds eq filter |
96
|
|
|
* |
97
|
|
|
* @param string $field |
98
|
|
|
* @param mixed $value |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
1 |
|
public function equal(string $field, $value) |
103
|
|
|
{ |
104
|
1 |
|
$this->expression->equal($field, $value); |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds ne filter |
111
|
|
|
* |
112
|
|
|
* @param string $field |
113
|
|
|
* @param mixed $value |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
2 |
|
public function notEqual(string $field, $value) |
118
|
|
|
{ |
119
|
2 |
|
$this->expression->notEqual($field, $value); |
120
|
|
|
|
121
|
2 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Adds gt filter |
126
|
|
|
* |
127
|
|
|
* @param string $field |
128
|
|
|
* @param mixed $value |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
|
133
|
|
|
public function greaterThan(string $field, $value) |
134
|
|
|
{ |
135
|
|
|
$this->expression->greaterThan($field,$value); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Adds gte filter |
142
|
|
|
* |
143
|
|
|
* @param string $field |
144
|
|
|
* @param mixed $value |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
|
149
|
|
|
public function greaterEqualThan(string $field, $value) |
150
|
|
|
{ |
151
|
|
|
$this->expression->greaterEqualThan($field,$value); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Adds lt filter |
158
|
|
|
* |
159
|
|
|
* @param string $field |
160
|
|
|
* @param mixed $value |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
|
165
|
|
|
public function lowerThan(string $field, $value) |
166
|
|
|
{ |
167
|
|
|
$this->expression->lowerThan($field,$value); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Adds lte filter |
174
|
|
|
* |
175
|
|
|
* @param string $field |
176
|
|
|
* @param mixed $value |
177
|
|
|
* |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
|
|
|
181
|
|
|
public function lowerEqualThan(string $field, $value) |
182
|
|
|
{ |
183
|
|
|
$this->expression->lowerEqualThan($field,$value); |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Adds in filter |
190
|
|
|
* |
191
|
|
|
* @param string $field |
192
|
|
|
* @param array $values |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
1 |
|
public function in(string $field, array $values) |
197
|
|
|
{ |
198
|
1 |
|
$this->expression->in($field, $values); |
199
|
|
|
|
200
|
1 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Adds nin filter |
205
|
|
|
* |
206
|
|
|
* @param string $field |
207
|
|
|
* @param array $values |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
1 |
|
public function notIn(string $field, array $values) |
212
|
|
|
{ |
213
|
1 |
|
$this->expression->notIn($field, $values); |
214
|
|
|
|
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns a new Query set up with the builder configuration |
220
|
|
|
* |
221
|
|
|
* @return Query |
222
|
|
|
*/ |
223
|
18 |
|
public function getQuery(): Query |
224
|
|
|
{ |
225
|
18 |
|
return new Query( |
226
|
18 |
|
$this->collection, |
227
|
18 |
|
$this->queryType, |
228
|
18 |
|
$this->expression->getExpressionFilters(), |
229
|
18 |
|
$this->options |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns the query type |
235
|
|
|
* |
236
|
|
|
* @return int |
237
|
|
|
*/ |
238
|
2 |
|
public function getQueryType(): int |
239
|
|
|
{ |
240
|
2 |
|
return $this->queryType; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets the sort option |
245
|
|
|
* |
246
|
|
|
* @param array $fields |
247
|
|
|
* |
248
|
|
|
* @return $this |
249
|
|
|
*/ |
250
|
2 |
|
public function sort(array $fields) |
251
|
|
|
{ |
252
|
2 |
|
return $this->setQueryOption('sort', $fields); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Sets the limit option |
257
|
|
|
* |
258
|
|
|
* @param int $limit |
259
|
|
|
* |
260
|
|
|
* @return $this |
261
|
|
|
*/ |
262
|
1 |
|
public function limit(int $limit) |
263
|
|
|
{ |
264
|
1 |
|
return $this->setQueryOption('limit', $limit); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Sets the skip(offset) option |
269
|
|
|
* |
270
|
|
|
* @param int $skip |
271
|
|
|
* |
272
|
|
|
* @return $this |
273
|
|
|
*/ |
274
|
1 |
|
public function skip(int $skip) |
275
|
|
|
{ |
276
|
1 |
|
return $this->setQueryOption('skip', $skip); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Sets the projection option |
281
|
|
|
* |
282
|
|
|
* @param array $projection |
283
|
|
|
* |
284
|
|
|
* @return $this |
285
|
|
|
*/ |
286
|
1 |
|
public function select(...$projection) |
287
|
|
|
{ |
288
|
1 |
|
$this->setQueryOption('projection', array_fill_keys($projection, 1)); |
|
|
|
|
289
|
|
|
|
290
|
1 |
|
if (!in_array('_id', $projection)) { |
291
|
1 |
|
$this->options['projection']['_id'] = -1; |
292
|
|
|
} |
293
|
|
|
|
294
|
1 |
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Sets the an actually unsupported option |
299
|
|
|
* @deprecated use the right method if supported |
300
|
|
|
* |
301
|
|
|
* @param string $option |
302
|
|
|
* @param mixed $value |
303
|
|
|
* |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
6 |
|
public function setQueryOption(string $option, $value) |
307
|
|
|
{ |
308
|
6 |
|
$this->options[$option] = $value; |
309
|
|
|
|
310
|
6 |
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns a new Expression |
315
|
|
|
* |
316
|
|
|
* @return Expression |
317
|
|
|
*/ |
318
|
3 |
|
public function expr(): Expression |
319
|
|
|
{ |
320
|
3 |
|
return new Expression(); |
321
|
|
|
} |
322
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.