|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Repository\Compilers; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Recca0120\Repository\Expression; |
|
8
|
|
|
|
|
9
|
|
|
class CollectionCompiler extends Compiler |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* where. |
|
13
|
|
|
* |
|
14
|
|
|
* @param mixed $model |
|
15
|
|
|
* @param array $actions |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
5 |
|
public function where($model, $actions) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$filters = []; |
|
21
|
5 |
|
foreach ($actions as $action) { |
|
22
|
5 |
|
$method = $action['method']; |
|
23
|
5 |
|
$parameters = $action['parameters']; |
|
24
|
5 |
|
if (count($parameters) === 3) { |
|
25
|
5 |
|
list($key, $operator, $value) = $parameters; |
|
26
|
5 |
|
} else { |
|
27
|
1 |
|
list($key, $value) = $parameters; |
|
28
|
1 |
|
$operator = '='; |
|
29
|
|
|
} |
|
30
|
5 |
|
if ($value instanceof Expression) { |
|
31
|
|
|
$value = $value->getValue(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
$filters[$method][] = [$key, $operator, $value]; |
|
35
|
5 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
$model = $model->filter(function ($item) use ($filters) { |
|
38
|
2 |
|
foreach ($filters as $method => $rules) { |
|
39
|
|
|
switch ($method) { |
|
40
|
2 |
|
case 'where': |
|
41
|
2 |
|
case 'having': |
|
42
|
2 |
|
foreach ($rules as $parameters) { |
|
43
|
2 |
|
if ($this->checkOperator($item, $parameters) === false) { |
|
44
|
1 |
|
return false; |
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
2 |
|
break; |
|
48
|
|
|
default: |
|
49
|
|
|
throw new BadMethodCallException('Call to undefined method '.static::class."::{$method}()"); |
|
50
|
|
|
} |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
return true; |
|
54
|
5 |
|
}); |
|
55
|
|
|
|
|
56
|
4 |
|
return $model; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* checkOperator. |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed$item |
|
63
|
|
|
* @param array $parameters |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
2 |
|
protected function checkOperator($item, $parameters) |
|
67
|
|
|
{ |
|
68
|
2 |
|
list($key, $operator, $value) = $parameters; |
|
69
|
2 |
|
$retrieved = $item[$key]; |
|
70
|
|
|
switch ($operator) { |
|
71
|
2 |
|
case '=': |
|
72
|
2 |
|
case '==': |
|
73
|
2 |
|
return $retrieved == $value; |
|
74
|
1 |
|
case '!=': |
|
75
|
1 |
|
case '<>': |
|
76
|
1 |
|
return $retrieved != $value; |
|
77
|
|
|
case '<': |
|
78
|
|
|
return $retrieved < $value; |
|
79
|
|
|
case '>': |
|
80
|
|
|
return $retrieved > $value; |
|
81
|
|
|
case '<=': |
|
82
|
|
|
return $retrieved <= $value; |
|
83
|
|
|
case '>=': |
|
84
|
|
|
return $retrieved >= $value; |
|
85
|
|
|
case '===': |
|
86
|
|
|
return $retrieved === $value; |
|
87
|
|
|
case '!==': |
|
88
|
|
|
return $retrieved !== $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* having. |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $model |
|
98
|
|
|
* @param array $actions |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function having($model, $actions) |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->where($model, $actions); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* order. |
|
108
|
|
|
* |
|
109
|
|
|
* @param mixed $model |
|
110
|
|
|
* @param array $actions |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function order($model, $actions) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$sort = []; |
|
116
|
1 |
|
foreach ($actions as $action) { |
|
117
|
1 |
|
$parameters = $action['parameters']; |
|
118
|
1 |
|
$sort[$parameters[0]] = Arr::get($parameters, 1, 'asc'); |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $model->sort($this->orderComparer($sort)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* with. |
|
126
|
|
|
* |
|
127
|
|
|
* @param mixed $model |
|
128
|
|
|
* @param array $actions |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function with($model, $actions) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
1 |
|
return $model; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* join. |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $model |
|
140
|
|
|
* @param array $actions |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function join($model, $actions) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
1 |
|
return $model; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* select. |
|
150
|
|
|
* |
|
151
|
|
|
* @param mixed $model |
|
152
|
|
|
* @param array $actions |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function select($model, $actions) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
1 |
|
return $model; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* orderComparer. |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $sort |
|
164
|
|
|
* @return \Closure |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function orderComparer($sort) |
|
167
|
|
|
{ |
|
168
|
1 |
|
return function ($first, $second) use ($sort) { |
|
169
|
1 |
|
foreach ($sort as $key => $orderType) { |
|
170
|
|
|
// normalize sort direction |
|
171
|
1 |
|
$orderType = strtolower($orderType); |
|
172
|
1 |
|
if (data_get($first, $key) < data_get($second, $key)) { |
|
173
|
1 |
|
return $orderType === 'asc' ? -1 : 1; |
|
174
|
1 |
|
} elseif (data_get($first, $key) > data_get($second, $key)) { |
|
175
|
1 |
|
return $orderType === 'asc' ? 1 : -1; |
|
176
|
|
|
} |
|
177
|
1 |
|
} |
|
178
|
|
|
// all elements were equal |
|
179
|
1 |
|
return 0; |
|
180
|
1 |
|
}; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* compileParameters. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $parameters |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
9 |
|
protected function compileParameters($parameters) |
|
190
|
|
|
{ |
|
191
|
9 |
|
return $parameters; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.