1 | <?php |
||
2 | |||
3 | namespace SleepingOwl\Admin\Traits; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Illuminate\Support\Arr; |
||
7 | use SleepingOwl\Admin\Exceptions\FilterOperatorException; |
||
8 | |||
9 | trait SqlQueryOperators |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $operator = 'equal'; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $sqlOperators = [ |
||
20 | 'equal' => ['method' => 'where', 'op' => '='], |
||
21 | 'not_equal' => ['method' => 'where', 'op' => '!='], |
||
22 | 'less' => ['method' => 'where', 'op' => '<'], |
||
23 | 'less_or_equal' => ['method' => 'where', 'op' => '<='], |
||
24 | 'greater' => ['method' => 'where', 'op' => '>'], |
||
25 | 'greater_or_equal' => ['method' => 'where', 'op' => '>='], |
||
26 | 'begins_with' => ['method' => 'where', 'op' => 'like', 'mod' => '?%'], |
||
27 | 'not_begins_with' => ['method' => 'where', 'op' => 'not like', 'mod' => '?%'], |
||
28 | 'contains' => ['method' => 'where', 'op' => 'like', 'mod' => '%?%'], |
||
29 | 'not_contains' => ['method' => 'where', 'op' => 'not like', 'mod' => '%?%'], |
||
30 | 'ends_with' => ['method' => 'where', 'op' => 'like', 'mod' => '%?'], |
||
31 | 'not_ends_with' => ['method' => 'where', 'op' => 'not like', 'mod' => '%?'], |
||
32 | 'is_empty' => ['method' => 'where', 'op' => '=', 'value' => ''], |
||
33 | 'is_not_empty' => ['method' => 'where', 'op' => '!=', 'value' => ''], |
||
34 | 'is_null' => ['method' => 'whereNull'], |
||
35 | 'is_not_null' => ['method' => 'whereNotNull'], |
||
36 | 'between' => ['method' => 'whereBetween'], |
||
37 | 'not_between' => ['method' => 'whereNotBetween'], |
||
38 | 'in' => ['method' => 'whereIn'], |
||
39 | 'not_in' => ['method' => 'whereNotIn'], |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | 40 | */ |
|
45 | public function getOperator() |
||
46 | 40 | { |
|
47 | return $this->operator; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $operator |
||
52 | * |
||
53 | * @return $this |
||
54 | * @throws FilterOperatorException |
||
55 | 40 | */ |
|
56 | public function setOperator($operator) |
||
57 | 40 | { |
|
58 | if (! array_key_exists($operator, $this->sqlOperators)) { |
||
59 | throw new FilterOperatorException("Operator [$operator] not found"); |
||
60 | } |
||
61 | 40 | ||
62 | $this->operator = $operator; |
||
63 | 40 | ||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param Builder $query |
||
69 | * @param string $column |
||
70 | * @param string|array $value |
||
71 | 40 | */ |
|
72 | protected function buildQuery(Builder $query, $column, $value) |
||
73 | 40 | { |
|
74 | 40 | $params = $this->getOperatorParams(); |
|
75 | 40 | $method = $params['method']; |
|
76 | $value = Arr::get($params, 'value', $value); |
||
77 | |||
78 | 40 | switch ($method) { |
|
79 | 28 | case 'where': |
|
80 | 28 | $value = str_replace('?', $value, Arr::get($params, 'mod', '?')); |
|
81 | 28 | $query->{$method}($column, $params['op'], $value); |
|
82 | 12 | break; |
|
83 | 12 | case 'whereNull': |
|
84 | 4 | case 'whereNotNull': |
|
85 | 4 | $query->{$method}($column); |
|
86 | 8 | break; |
|
87 | 8 | case 'whereBetween': |
|
88 | 4 | case 'whereNotBetween': |
|
89 | 4 | $query->{$method}($column, (array) $value); |
|
90 | 4 | break; |
|
91 | 4 | case 'whereIn': |
|
92 | case 'whereNotIn': |
||
0 ignored issues
–
show
|
|||
93 | 4 | ||
94 | 4 | $query->{$method}($column, (array) $value); |
|
95 | break; |
||
96 | 40 | } |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | 40 | */ |
|
102 | protected function getOperatorParams() |
||
103 | 40 | { |
|
104 | return Arr::get($this->sqlOperators, $this->getOperator(), ['method' => 'where', 'op' => '=']); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string|array $value |
||
109 | * |
||
110 | * @return string|array |
||
111 | */ |
||
112 | protected function prepareValue($value) |
||
113 | { |
||
114 | $params = $this->getOperatorParams(); |
||
115 | $method = $params['method']; |
||
116 | switch ($method) { |
||
117 | case 'where': |
||
118 | case 'whereNull': |
||
119 | case 'whereNotNull': |
||
120 | break; |
||
121 | case 'whereBetween': |
||
122 | case 'whereNotBetween': |
||
123 | if (! is_array($value)) { |
||
124 | $value = explode(',', $value, 2); |
||
125 | } |
||
126 | break; |
||
127 | case 'whereIn': |
||
128 | case 'whereNotIn': |
||
129 | if (! is_array($value)) { |
||
130 | $value = explode(',', $value); |
||
131 | } |
||
132 | break; |
||
133 | } |
||
134 | |||
135 | return $value; |
||
136 | } |
||
137 | } |
||
138 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.