1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HChamran\LaravelFilter\Contracts; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
7
|
|
|
|
8
|
|
|
abstract class QueryFilter implements FilterInterface |
9
|
|
|
{ |
10
|
|
|
protected $request; |
11
|
|
|
|
12
|
|
|
protected $fields; |
13
|
|
|
|
14
|
|
|
protected $sort; |
15
|
|
|
|
16
|
|
|
protected $filterString; |
17
|
|
|
|
18
|
|
|
protected $filters; |
19
|
|
|
|
20
|
|
|
protected $builder; |
21
|
|
|
|
22
|
|
|
protected $chars = ['-']; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->fields = $this->fields(); |
27
|
|
|
|
28
|
|
|
$this->request = Request::capture(); |
29
|
|
|
|
30
|
|
|
$this->requestParser($this->getRequests()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Builder $query |
35
|
|
|
* @return Builder |
36
|
|
|
*/ |
37
|
|
|
public function builder(Builder $query) |
38
|
|
|
{ |
39
|
|
|
if (is_null($this->filters)) |
40
|
|
|
return $query; |
41
|
|
|
foreach ($this->filters as $filter) { |
42
|
|
|
if (is_array($filter[1])) { |
43
|
|
|
$query = $query->whereBetween($filter[0], $filter[1]); |
44
|
|
|
} else { |
45
|
|
|
$query = $query->where($filter[0], 'like', "%{$filter[1]}%"); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->setSortFilter($query); |
50
|
|
|
|
51
|
|
|
return $query; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all requests |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function getRequests() |
60
|
|
|
{ |
61
|
|
|
return $this->request->all(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Parse get url string |
66
|
|
|
* |
67
|
|
|
* @param $request |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
private function requestParser($request) |
71
|
|
|
{ |
72
|
|
|
if (empty($request)) |
73
|
|
|
return; |
74
|
|
|
|
75
|
|
|
$this->chackSortFilter($request); |
76
|
|
|
|
77
|
|
|
$filters = $this->getFilters(); |
78
|
|
|
|
79
|
|
|
$this->setFieldsExists($filters); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if sort query string exist get it attributes |
84
|
|
|
* |
85
|
|
|
* @param $request |
86
|
|
|
*/ |
87
|
|
|
private function chackSortFilter($request) |
88
|
|
|
{ |
89
|
|
|
if (strpos(array_keys($request)[0], 'asc') || strpos(array_keys($request)[0], 'desc')) { |
90
|
|
|
list($this->filterString, $this->sort) = explode(',', array_keys($request)[0]); |
91
|
|
|
if (strpos($this->sort, 'By')) |
92
|
|
|
$this->sort = explode('By', $this->sort); |
93
|
|
|
else { |
94
|
|
|
$this->sort = str_split($this->sort, 4); |
95
|
|
|
$this->sort[] = $this->getStringFilters(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
else |
100
|
|
|
$this->filterString = array_keys($request)[0]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set sort of result |
105
|
|
|
* |
106
|
|
|
* @param $query |
107
|
|
|
* @return Builder |
108
|
|
|
*/ |
109
|
|
|
private function setSortFilter($query) |
110
|
|
|
{ |
111
|
|
|
if (is_null($this->sort)) |
112
|
|
|
return $query; |
113
|
|
|
else |
114
|
|
|
return $query->orderBy($this->sort[1], $this->sort[0]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get feild of query string |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
private function getStringFilters() |
123
|
|
|
{ |
124
|
|
|
return explode(':', explode(',', array_keys($this->getRequests())[0])[0])[0]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return value of get string request |
129
|
|
|
* |
130
|
|
|
* @param $conditions |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
private function getFilters() |
134
|
|
|
{ |
135
|
|
|
$conditions = explode('|', $this->filterString); |
136
|
|
|
|
137
|
|
|
foreach ($conditions as $filter) { |
138
|
|
|
$this->filters[] = explode(':', $filter); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->filters; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* This function returned just values which exists in fields property |
146
|
|
|
* |
147
|
|
|
* @param $filters |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function setFieldsExists($filters) |
151
|
|
|
{ |
152
|
|
|
$current = 0; |
153
|
|
|
foreach ($filters as $filter) { |
154
|
|
|
if ($this->fieldExist($filter[0])) { |
155
|
|
|
if ($this->charExist($filter[1], '-')) { |
156
|
|
|
$filters[$current][1] = explode('-', $filter[1]); |
157
|
|
|
} |
158
|
|
|
$current++; |
159
|
|
|
} |
160
|
|
|
else { |
161
|
|
|
unset($filters[$current]); |
162
|
|
|
$current++; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$this->filters = $filters; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return if field exist in fields property |
170
|
|
|
* |
171
|
|
|
* @param $filed |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
private function fieldExist($filed) |
175
|
|
|
{ |
176
|
|
|
return in_array($filed, $this->fields); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Check if needle|character in field exist |
181
|
|
|
* |
182
|
|
|
* @param $field |
183
|
|
|
* @param $needle |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
private function charExist($field, $needle) |
187
|
|
|
{ |
188
|
|
|
foreach ($this->chars as $char) { |
189
|
|
|
if ($needle == $char && strpos($field, $char)) { |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths