1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Algatux\MongoDB\QueryBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Expression. |
7
|
|
|
*/ |
8
|
|
|
class Expression |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
private $filters; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Expression constructor. |
15
|
|
|
*/ |
16
|
24 |
|
public function __construct() |
17
|
|
|
{ |
18
|
24 |
|
$this->filters = []; |
19
|
24 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Adds and filter |
23
|
|
|
* |
24
|
|
|
* @param array|Expression $expression |
25
|
|
|
* |
26
|
|
|
* @return $this|Expression |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function and($expression): Expression |
|
|
|
|
29
|
|
|
{ |
30
|
9 |
|
$this->prepareFilterIndex('$and'); |
31
|
|
|
|
32
|
9 |
|
$this->filters['$and'] = array_merge( |
33
|
9 |
|
$this->filters['$and'], |
34
|
9 |
|
$this->mapExpressions(...func_get_args()) |
35
|
|
|
); |
36
|
|
|
|
37
|
9 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $operator |
42
|
|
|
*/ |
43
|
19 |
|
private function prepareFilterIndex(string $operator) |
44
|
|
|
{ |
45
|
19 |
|
if (!isset($this->filters[$operator])) { |
46
|
19 |
|
$this->filters[$operator] = []; |
47
|
|
|
} |
48
|
19 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $expressions |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
10 |
|
private function mapExpressions($expressions): array |
|
|
|
|
56
|
|
|
{ |
57
|
10 |
|
return array_map( |
58
|
|
|
function ($expression) { |
59
|
10 |
|
return $expression instanceof Expression ? $expression->getExpressionFilters() : $expression; |
60
|
10 |
|
}, |
61
|
|
|
func_get_args() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the filters generated by the expression |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
24 |
|
public function getExpressionFilters(): array |
71
|
|
|
{ |
72
|
24 |
|
return $this->filters; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds or filter |
77
|
|
|
* |
78
|
|
|
* @param array|Expression $expression |
79
|
|
|
* |
80
|
|
|
* @return $this|Expression |
81
|
|
|
*/ |
82
|
3 |
View Code Duplication |
public function or ($expression): Expression |
|
|
|
|
83
|
|
|
{ |
84
|
3 |
|
$this->prepareFilterIndex('$or'); |
85
|
|
|
|
86
|
3 |
|
$this->filters['$or'] = array_merge( |
87
|
3 |
|
$this->filters['$or'], |
88
|
3 |
|
$this->mapExpressions(...func_get_args()) |
89
|
|
|
); |
90
|
|
|
|
91
|
3 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds eq filter |
96
|
|
|
* |
97
|
|
|
* @param string $field |
98
|
|
|
* @param string $value |
99
|
|
|
* |
100
|
|
|
* @return $this|Expression |
101
|
|
|
*/ |
102
|
2 |
|
public function equal(string $field, $value): Expression |
103
|
|
|
{ |
104
|
2 |
|
$this->prepareFilterIndex($field); |
105
|
|
|
|
106
|
2 |
|
$this->filters[$field] = $this->operationExpression('$eq', $value); |
107
|
|
|
|
108
|
2 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Adds ne filter |
113
|
|
|
* |
114
|
|
|
* @param string $field |
115
|
|
|
* @param string $value |
116
|
|
|
* |
117
|
|
|
* @return $this|Expression |
118
|
|
|
*/ |
119
|
4 |
|
public function notEqual(string $field, $value): Expression |
120
|
|
|
{ |
121
|
4 |
|
$this->prepareFilterIndex($field); |
122
|
|
|
|
123
|
4 |
|
$this->filters[$field] = $this->operationExpression('$ne', $value); |
124
|
|
|
|
125
|
4 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds gt filter |
130
|
|
|
* |
131
|
|
|
* @param string $field |
132
|
|
|
* @param string $value |
133
|
|
|
* |
134
|
|
|
* @return $this|Expression |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
public function greaterThan(string $field, $value): Expression |
138
|
|
|
{ |
139
|
|
|
$this->prepareFilterIndex($field); |
140
|
|
|
|
141
|
|
|
$this->filters[$field] = $this->operationExpression('$gt', $value); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Adds gte filter |
148
|
|
|
* |
149
|
|
|
* @param string $field |
150
|
|
|
* @param string $value |
151
|
|
|
* |
152
|
|
|
* @return $this|Expression |
153
|
|
|
*/ |
154
|
|
|
|
155
|
|
|
public function greaterEqualThan(string $field, $value): Expression |
156
|
|
|
{ |
157
|
|
|
$this->prepareFilterIndex($field); |
158
|
|
|
|
159
|
|
|
$this->filters[$field] = $this->operationExpression('$gte', $value); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Adds lt filter |
166
|
|
|
* |
167
|
|
|
* @param string $field |
168
|
|
|
* @param string $value |
169
|
|
|
* |
170
|
|
|
* @return $this|Expression |
171
|
|
|
*/ |
172
|
|
|
|
173
|
|
|
public function lowerThan(string $field, $value): Expression |
174
|
|
|
{ |
175
|
|
|
$this->prepareFilterIndex($field); |
176
|
|
|
|
177
|
|
|
$this->filters[$field] = $this->operationExpression('$lt', $value); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Adds lte filter |
184
|
|
|
* |
185
|
|
|
* @param string $field |
186
|
|
|
* @param string $value |
187
|
|
|
* |
188
|
|
|
* @return $this|Expression |
189
|
|
|
*/ |
190
|
|
|
|
191
|
|
|
public function lowerEqualThan(string $field, $value): Expression |
192
|
|
|
{ |
193
|
|
|
$this->prepareFilterIndex($field); |
194
|
|
|
|
195
|
|
|
$this->filters[$field] = $this->operationExpression('$lte', $value); |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Adds in filter |
202
|
|
|
* |
203
|
|
|
* @param string $field |
204
|
|
|
* @param array $values |
205
|
|
|
* |
206
|
|
|
* @return $this|Expression |
207
|
|
|
*/ |
208
|
2 |
|
public function in(string $field, array $values): Expression |
209
|
|
|
{ |
210
|
2 |
|
$this->prepareFilterIndex($field); |
211
|
|
|
|
212
|
2 |
|
$this->filters[$field] = $this->operationExpression('$in', $values); |
213
|
|
|
|
214
|
2 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Adds notIn filter |
219
|
|
|
* |
220
|
|
|
* @param string $field |
221
|
|
|
* @param array $values |
222
|
|
|
* |
223
|
|
|
* @return $this|Expression |
224
|
|
|
*/ |
225
|
2 |
|
public function notIn(string $field, array $values): Expression |
226
|
|
|
{ |
227
|
2 |
|
$this->prepareFilterIndex($field); |
228
|
|
|
|
229
|
2 |
|
$this->filters[$field] = $this->operationExpression('$nin', $values); |
230
|
|
|
|
231
|
2 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $operation |
237
|
|
|
* @param mixed $value |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
10 |
|
private function operationExpression(string $operation, $value): array |
242
|
|
|
{ |
243
|
10 |
|
return [$operation => $value]; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.