-
Notifications
You must be signed in to change notification settings - Fork 11
/
QueryProcessor.php
518 lines (454 loc) · 14.9 KB
/
QueryProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php
/**
* @see https://github.com/yii2tech
*
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2mod\query;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
/**
* QueryProcessor processes [[Query]] instances adjusting data accordingly.
* It applies filter conditions, sorting and limit.
*
* @see Query
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @author Igor Chepurnoy <igorzfort@gmail.com>
*
* @since 1.0
*/
class QueryProcessor extends Component
{
/**
* @var array
*/
protected $conditionFilters = [
'NOT' => 'filterNotCondition',
'AND' => 'filterAndCondition',
'OR' => 'filterOrCondition',
'BETWEEN' => 'filterBetweenCondition',
'NOT BETWEEN' => 'filterBetweenCondition',
'IN' => 'filterInCondition',
'NOT IN' => 'filterInCondition',
'LIKE' => 'filterLikeCondition',
'NOT LIKE' => 'filterLikeCondition',
'OR LIKE' => 'filterLikeCondition',
'OR NOT LIKE' => 'filterLikeCondition',
'CALLBACK' => 'filterCallbackCondition',
];
/**
* @var ArrayQuery
*/
private $_query;
/**
* @param ArrayQuery $query
*
* @return array[]
*/
public function process($query)
{
$this->_query = $query;
$data = $this->_query->from;
$data = $this->applyWhere($data, $this->_query->where);
$data = $this->applyOrderBy($data, $this->_query->orderBy);
$data = $this->applyLimit($data, $this->_query->limit, $this->_query->offset);
return $data;
}
/**
* Applies sort for given data.
*
* @param array $data raw data
* @param array|null $orderBy order by
*
* @return array sorted data
*/
protected function applyOrderBy(array $data, $orderBy)
{
if (!empty($orderBy)) {
ArrayHelper::multisort($data, array_keys($orderBy), array_values($orderBy));
}
return $data;
}
/**
* Applies limit and offset for given data.
*
* @param array $data raw data
* @param int|null $limit limit value
* @param int|null $offset offset value
*
* @return array data
*/
protected function applyLimit(array $data, $limit, $offset)
{
if (empty($limit) && empty($offset)) {
return $data;
}
if (!ctype_digit((string) $limit)) {
$limit = null;
}
if (!ctype_digit((string) $offset)) {
$offset = 0;
}
return array_slice($data, $offset, $limit);
}
/**
* Applies where conditions.
*
* @param array $data raw data
* @param array|null $where where conditions
*
* @return array data
*/
protected function applyWhere(array $data, $where)
{
return $this->filterCondition($data, $where);
}
/**
* Applies filter conditions.
*
* @param array $data data to be filtered
* @param array $condition filter condition
*
* @return array filtered data
*
* @throws InvalidParamException
*/
public function filterCondition(array $data, $condition)
{
if (empty($condition)) {
return $data;
}
if (!is_array($condition)) {
throw new InvalidParamException('Condition must be an array');
}
if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
$operator = strtoupper($condition[0]);
if (isset($this->conditionFilters[$operator])) {
$method = $this->conditionFilters[$operator];
} else {
$method = 'filterSimpleCondition';
}
array_shift($condition);
return $this->$method($data, $operator, $condition);
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return $this->filterHashCondition($data, $condition);
}
}
/**
* Applies a condition based on column-value pairs.
*
* @param array $data data to be filtered
* @param array $condition the condition specification
*
* @return array filtered data
*/
public function filterHashCondition(array $data, $condition)
{
foreach ($condition as $column => $value) {
if (is_array($value)) {
// IN condition
$data = $this->filterInCondition($data, 'IN', [$column, $value]);
} else {
$data = array_filter($data, function ($row) use ($column, $value) {
if ($value instanceof \Closure) {
return call_user_func($value, $row[$column]);
}
return $row[$column] == $value;
});
}
}
return $data;
}
/**
* Applies 2 or more conditions using 'AND' logic.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands conditions to be united
*
* @return array filtered data
*/
protected function filterAndCondition(array $data, $operator, $operands)
{
foreach ($operands as $operand) {
if (is_array($operand)) {
$data = $this->filterCondition($data, $operand);
}
}
return $data;
}
/**
* Applies 2 or more conditions using 'OR' logic.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands conditions to be united
*
* @return array filtered data
*/
protected function filterOrCondition(array $data, $operator, $operands)
{
$parts = [];
foreach ($operands as $operand) {
if (is_array($operand)) {
$parts[] = $this->filterCondition($data, $operand);
}
}
if (empty($parts)) {
return $data;
}
$data = [];
foreach ($parts as $part) {
foreach ($part as $row) {
$pk = $row[$this->_query->primaryKeyName];
$data[$pk] = $row;
}
}
return $data;
}
/**
* Inverts a filter condition.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands operands to be inverted
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given
*/
protected function filterNotCondition(array $data, $operator, $operands)
{
if (count($operands) != 1) {
throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
}
$operand = reset($operands);
$filteredData = $this->filterCondition($data, $operand);
if (empty($filteredData)) {
return $data;
}
$pkName = $this->_query->primaryKeyName;
foreach ($data as $key => $row) {
foreach ($filteredData as $filteredRowKey => $filteredRow) {
if ($row[$pkName] === $filteredRow[$pkName]) {
unset($data[$key]);
unset($filteredData[$filteredRowKey]);
break;
}
}
}
return $data;
}
/**
* Applies `BETWEEN` condition.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands the first operand is the column name. The second and third operands
* describe the interval that column value should be in
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given
*/
protected function filterBetweenCondition(array $data, $operator, $operands)
{
if (!isset($operands[0], $operands[1], $operands[2])) {
throw new InvalidParamException("Operator '$operator' requires three operands.");
}
list($column, $value1, $value2) = $operands;
if (strncmp('NOT', $operator, 3) === 0) {
return array_filter($data, function ($row) use ($column, $value1, $value2) {
return $row[$column] < $value1 || $row[$column] > $value2;
});
}
return array_filter($data, function ($row) use ($column, $value1, $value2) {
return $row[$column] >= $value1 && $row[$column] <= $value2;
});
}
/**
* Applies 'IN' condition.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands the first operand is the column name.
* The second operand is an array of values that column value should be among
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given
*/
protected function filterInCondition(array $data, $operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $values) = $operands;
if ($values === [] || $column === []) {
return $operator === 'IN' ? [] : $data;
}
$values = (array) $values;
if (count((array) $column) > 1) {
throw new InvalidParamException("Operator '$operator' allows only a single column.");
}
if (is_array($column)) {
$column = reset($column);
}
foreach ($values as $i => $value) {
if (is_array($value)) {
$values[$i] = isset($value[$column]) ? $value[$column] : null;
}
}
if (strncmp('NOT', $operator, 3) === 0) {
return array_filter($data, function ($row) use ($column, $values) {
return !in_array($row[$column], $values);
});
}
return array_filter($data, function ($row) use ($column, $values) {
return in_array($row[$column], $values);
});
}
/**
* Applies 'LIKE' condition.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands the first operand is the column name. The second operand is a single value
* or an array of values that column value should be compared with
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given
*/
protected function filterLikeCondition(array $data, $operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $values) = $operands;
if (!is_array($values)) {
$values = [$values];
}
$not = (stripos($operator, 'NOT ') !== false);
$or = (stripos($operator, 'OR ') !== false);
if ($not) {
if (empty($values)) {
return $data;
}
if ($or) {
return array_filter($data, function ($row) use ($column, $values) {
foreach ($values as $value) {
if (stripos($row[$column], $value) === false) {
return true;
}
}
return false;
});
}
return array_filter($data, function ($row) use ($column, $values) {
foreach ($values as $value) {
if (stripos($row[$column], $value) !== false) {
return false;
}
}
return true;
});
}
if (empty($values)) {
return [];
}
if ($or) {
return array_filter($data, function ($row) use ($column, $values) {
foreach ($values as $value) {
if (stripos($row[$column], $value) !== false) {
return true;
}
}
return false;
});
}
return array_filter($data, function ($row) use ($column, $values) {
foreach ($values as $value) {
if (stripos($row[$column], $value) === false) {
return false;
}
}
return true;
});
}
/**
* Applies 'CALLBACK' condition.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands the only one operand is the PHP callback, which should be compatible with
* `array_filter()` PHP function, e.g.:
*
* ```php
* function ($row) {
* //return bool whether row matches condition or not
* }
* ```
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given
*
* @since 1.0.3
*/
public function filterCallbackCondition(array $data, $operator, $operands)
{
if (count($operands) != 1) {
throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
}
$callback = reset($operands);
return array_filter($data, $callback);
}
/**
* Applies comparison condition, e.g. `column operator value`.
*
* @param array $data data to be filtered
* @param string $operator operator
* @param array $operands
*
* @return array filtered data
*
* @throws InvalidParamException if wrong number of operands have been given or operator is not supported
*
* @since 1.0.4
*/
public function filterSimpleCondition(array $data, $operator, $operands)
{
if (count($operands) !== 2) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
return array_filter($data, function ($row) use ($operator, $column, $value) {
switch ($operator) {
case '=':
case '==':
return $row[$column] == $value;
case '===':
return $row[$column] === $value;
case '!=':
case '<>':
return $row[$column] != $value;
case '!==':
return $row[$column] !== $value;
case '>':
return $row[$column] > $value;
case '<':
return $row[$column] < $value;
case '>=':
return $row[$column] >= $value;
case '<=':
return $row[$column] <= $value;
default:
throw new InvalidParamException("Operator '$operator' is not supported.");
}
});
}
}