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
|
|
|
} 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
|
|
|
} |
36
|
|
|
|
37
|
|
|
$model = $model->filter(function ($item) use ($filters) { |
38
|
2 |
|
foreach ($filters as $method => $rules) { |
39
|
|
|
switch ($method) { |
40
|
2 |
|
case 'where': |
41
|
1 |
|
case 'having': |
42
|
2 |
|
foreach ($rules as $parameters) { |
43
|
2 |
|
if ($this->checkOperator($item, $parameters) === false) { |
44
|
1 |
|
return false; |
45
|
|
|
} |
46
|
|
|
} |
47
|
2 |
|
break; |
48
|
|
|
default: |
49
|
|
|
throw new BadMethodCallException('Call to undefined method '.static::class."::{$method}()"); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return true; |
54
|
5 |
|
}); |
55
|
|
|
|
56
|
4 |
|
return $model; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* having. |
61
|
|
|
* |
62
|
|
|
* @param mixed $model |
63
|
|
|
* @param array $actions |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
1 |
|
public function having($model, $actions) |
67
|
|
|
{ |
68
|
1 |
|
return $this->where($model, $actions); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* order. |
73
|
|
|
* |
74
|
|
|
* @param mixed $model |
75
|
|
|
* @param array $actions |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
1 |
|
public function order($model, $actions) |
79
|
|
|
{ |
80
|
1 |
|
$sort = []; |
81
|
1 |
|
foreach ($actions as $action) { |
82
|
1 |
|
$parameters = $action['parameters']; |
83
|
1 |
|
$sort[$parameters[0]] = Arr::get($parameters, 1, 'asc'); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $model->sort($this->orderComparer($sort)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* with. |
91
|
|
|
* |
92
|
|
|
* @param mixed $model |
93
|
|
|
* @param array $actions |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
1 |
|
public function with($model, $actions) |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
return $model; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* join. |
103
|
|
|
* |
104
|
|
|
* @param mixed $model |
105
|
|
|
* @param array $actions |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
1 |
|
public function join($model, $actions) |
|
|
|
|
109
|
|
|
{ |
110
|
1 |
|
return $model; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* select. |
115
|
|
|
* |
116
|
|
|
* @param mixed $model |
117
|
|
|
* @param array $actions |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
1 |
|
public function select($model, $actions) |
|
|
|
|
121
|
|
|
{ |
122
|
1 |
|
return $model; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* checkOperator. |
127
|
|
|
* |
128
|
|
|
* @param mixed$item |
129
|
|
|
* @param array $parameters |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
2 |
|
protected function checkOperator($item, $parameters) |
133
|
|
|
{ |
134
|
2 |
|
list($key, $operator, $value) = $parameters; |
135
|
2 |
|
$retrieved = $item[$key]; |
136
|
|
|
switch ($operator) { |
137
|
2 |
|
case '=': |
138
|
1 |
|
case '==': |
139
|
2 |
|
return $retrieved == $value; |
140
|
1 |
|
case '!=': |
141
|
|
|
case '<>': |
142
|
1 |
|
return $retrieved != $value; |
143
|
|
|
case '<': |
144
|
|
|
return $retrieved < $value; |
145
|
|
|
case '>': |
146
|
|
|
return $retrieved > $value; |
147
|
|
|
case '<=': |
148
|
|
|
return $retrieved <= $value; |
149
|
|
|
case '>=': |
150
|
|
|
return $retrieved >= $value; |
151
|
|
|
case '===': |
152
|
|
|
return $retrieved === $value; |
153
|
|
|
case '!==': |
154
|
|
|
return $retrieved !== $value; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
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 |
|
$value1 = data_get($first, $key); |
173
|
1 |
|
$value2 = data_get($second, $key); |
174
|
|
|
|
175
|
1 |
|
if ($value1 < $value2) { |
176
|
1 |
|
return $orderType === 'asc' ? -1 : 1; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
if ($value1 > $value2) { |
180
|
1 |
|
return $orderType === 'asc' ? 1 : -1; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
// all elements were equal |
184
|
1 |
|
return 0; |
185
|
1 |
|
}; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* compileParameters. |
190
|
|
|
* |
191
|
|
|
* @param array $parameters |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
9 |
|
protected function compileParameters($parameters) |
195
|
|
|
{ |
196
|
9 |
|
return $parameters; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.