|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\condition; |
|
4
|
|
|
|
|
5
|
|
|
class ConditionMicroDbParser |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @param Condition|null $condition |
|
10
|
|
|
* @param int|null $limit |
|
11
|
|
|
* @param int|null $offset |
|
12
|
|
|
* |
|
13
|
|
|
* @return \Closure|null |
|
14
|
|
|
*/ |
|
15
|
|
|
public function filter(Condition $condition = null, int &$limit = null, int &$offset = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$offset = (int)$offset; |
|
18
|
|
|
if ($condition) { |
|
19
|
|
|
$limit = $condition->getLimit(); |
|
20
|
|
|
$offset = (int)$condition->getOffset(); |
|
21
|
|
|
$conditions = $condition->getConditions(); |
|
22
|
|
|
return function (array $item) use ($conditions) { |
|
23
|
|
|
foreach ($conditions as $cond) { |
|
24
|
|
|
if (!\is_array($cond)) { |
|
25
|
|
|
continue; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$cond = \array_values($cond); |
|
29
|
|
|
$type = \array_shift($cond); |
|
30
|
|
|
switch ($type) { |
|
31
|
|
View Code Duplication |
case Condition::EQUALS: |
|
|
|
|
|
|
32
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */ |
|
33
|
|
|
if ($item[$cond[0]] != $cond[1]) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
break; |
|
37
|
|
View Code Duplication |
case Condition::NOT_EQUALS: |
|
|
|
|
|
|
38
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */ |
|
39
|
|
|
if ($item[$cond[0]] == $cond[1]) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
break; |
|
43
|
|
View Code Duplication |
case Condition::LESS_THAN: |
|
|
|
|
|
|
44
|
|
|
if ($item[$cond[0]] >= $cond[1]) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
break; |
|
48
|
|
View Code Duplication |
case Condition::GREATER_THAN: |
|
|
|
|
|
|
49
|
|
|
if ($item[$cond[0]] <= $cond[1]) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
break; |
|
53
|
|
View Code Duplication |
case Condition::LESS_OR_EQUALS: |
|
|
|
|
|
|
54
|
|
|
if ($item[$cond[0]] > $cond[1]) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
break; |
|
58
|
|
View Code Duplication |
case Condition::GREATER_OR_EQUALS: |
|
|
|
|
|
|
59
|
|
|
if ($item[$cond[0]] < $cond[1]) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
break; |
|
63
|
|
|
case Condition::BETWEEN: |
|
64
|
|
|
if ($item[$cond[0]] < $cond[1] || $item[$cond[0]] > $cond[2]) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
break; |
|
68
|
|
|
case Condition::LIKE: |
|
69
|
|
|
if (!\mb_strstr($item[$cond[0]], $cond[1])) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
break; |
|
73
|
|
|
case Condition::IN: |
|
74
|
|
|
if (!\in_array($item[$cond[0]], $cond[1], false)) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
}; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Condition|null $condition |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Closure|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function sort(Condition $condition = null) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($condition) { |
|
96
|
|
|
$sort = $condition->getSort(); |
|
97
|
|
|
return function (array $item1, array $item2) use ($sort) { |
|
98
|
|
|
foreach ($sort as $field => $dir) { |
|
99
|
|
|
$value1 = $item1[$field] ?? null; |
|
100
|
|
|
$value2 = $item2[$field] ?? null; |
|
101
|
|
|
if ($value1 === $value2) { |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
return ($value1 <=> $value2) * ($dir === \SORT_ASC ? 1 : -1); |
|
105
|
|
|
} |
|
106
|
|
|
return 0; |
|
107
|
|
|
}; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.