-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRequestQueryBuildable.php
More file actions
322 lines (271 loc) · 7.91 KB
/
RequestQueryBuildable.php
File metadata and controls
322 lines (271 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace Laravelha\Support\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
trait RequestQueryBuildable
{
/**
* Store columns on select
*
* @var array
*/
private static $select = [];
/**
* Store relations
*
* @var array
*/
private static $queryStringRelations = [];
/**
* All of the available operators.
*
* @var array
*/
public static $operators = [
'=', '<', '>', '<=', '>=', '<>', '!=', '<=>',
'like', 'like binary', 'not like', 'ilike',
'&', '|', '^', '<<', '>>',
'rlike', 'not rlike', 'regexp', 'not regexp',
'~', '~*', '!~', '!~*', 'similar to',
'not similar to', 'not ilike', '~~*', '!~~*',
];
/**
* @return array
*/
abstract public static function searchable() : array;
/**
* Trait bootable
* @return void
*/
public static function bootRequestQueryBuildable(): void
{
array_push(self::$select, self::getTableName().'.*');
self::applyOnly();
self::applySearch();
self::applySorts();
self::getWith();
self::applyRelations();
}
/**
* Get query string to only 'only=field1;field2;field3'
*
* @return array|null
*/
private static function getOnly() : ?array
{
if (! $only = request()->get('only', null))
return null;
static::setSelect($only = explode(';', $only));
self::setQueryStringRelations($only);
return $only;
}
/**
* Filter columns on select
*
* @return void
*/
private static function applyOnly() : void
{
if (! $only = self::getOnly())
return;
static::addGlobalScope('only', function (Builder $builder) {
$builder->select(self::$select);
});
}
/**
* Filter columns on select
*
* @param array $fields
* @return void
*/
private static function setSelect(array $fields) : void
{
self::$select = [];
foreach ($fields as $field) {
if (!strpos($field, '.'))
array_push(self::$select, $field);
}
}
/**
* Get query string searchable 'searchable=field1:operator1;field2:operator2;field3:operator3'
*
* @return array|null
*/
private static function getOperators() : ?array
{
if (! $operators = request()->get('operators', null))
return null;
$operators = explode(';', $operators);
$fieldOperator = [];
foreach ($operators as $operator) {
$parts = explode(':', $operator);
$fieldOperator[] = $parts[0];
$fieldOperator[$parts[0]] = $parts[1];
}
return $fieldOperator;
}
/**
* Get filed searchable on query string 'search=field1;fie
*
* @return array
*/
private static function getSearch() : array
{
$searchBy = request()->get('search', []);
if (!empty($searchBy)) {
return explode(';', $searchBy);
}
return $searchBy;
}
/**
* Apply searchable as global scope to where
*
* @return void
*/
private static function applySearch() : void
{
$searchBy = self::getSearch();
$searchable = self::getOperators() ?? static::searchable();
foreach ($searchBy as $item) {
$item = explode(':', $item);
if (array_key_exists($item[0], $searchable)) {
static::addGlobalScope($item[0], function (Builder $builder) use ($item, $searchable) {
$operatorValue = static::prepareOperatorValue($searchable[$item[0]], $item[1]);
if(!strpos($item[0], '.')) {
$builder->where($item[0], $operatorValue['operator'], $operatorValue['value']);
} else {
$relation = explode('.', $item[0]);
$builder->whereHas($relation[0], function ($query) use ($operatorValue, $item, $relation) {
$query->where($relation[1], $operatorValue['operator'], $operatorValue['value']);
});
}
});
}
}
}
/**
* Explode query string sort 'sort=field:direction'
*
* @return array
*/
private static function getSorts() : array
{
$sorts = request()->get('sort', []);
if(!empty($sorts))
return explode(';', $sorts);
return $sorts;
}
/**
* Apply global scope to sort (order by)
*
* @return void
*/
private static function applySorts() : void
{
if (! $sorts = self::getSorts())
return;
foreach ($sorts as $sort) {
$sort = explode(':', $sort);
if(strpos($sort[0], '.'))
continue;
static::addGlobalScope('sort', function (Builder $builder) use ($sort) {
$direction = isset($sort[1]) ? $sort[1] : 'asc';
$builder->orderBy($sort[0], $direction);
});
}
}
/**
* @param array $fields
* @return void
*/
private static function setQueryStringRelations(array $fields): void
{
foreach ($fields as $field) {
if(strpos($field, '.')) {
unset(self::$select[$field]);
$field = explode('.', $field);
array_push(self::$select, $field[0].'_id');
if(in_array($field[0], self::$queryStringRelations))
array_push(self::$queryStringRelations[$field[0]], $field[1]);
else
self::$queryStringRelations[$field[0]][] = $field[1];
}
}
}
/**
* Get query string to with 'with=field1;field2;field3'
*
* @return void
*/
private static function getWith() : void
{
if (! $with = request()->get('with', null))
return;
$relations = explode(';', $with);
foreach ($relations as $relation) {
self::$queryStringRelations[$relation] = [Str::plural(Str::snake($relation)).'.*'];
}
}
/**
* Apply global scope to relations
*
* @return void
*/
private static function applyRelations() : void
{
if (empty($relations = self::$queryStringRelations))
return;
static::addGlobalScope('relations', function (Builder $builder) use ($relations) {
$builder->select(self::$select)
->with(self::getWithRelation($relations));
});
}
/**
* Get operator and value prepared
*
* @param array $relations
* @return array
*/
private static function getWithRelation(array $relations): array
{
foreach ($relations as $relationName => $fields) {
if (!in_array('id', $fields))
array_push($fields, 'id');
$relations[$relationName] = function ($query) use ($fields) {
$query->select($fields)->withoutGlobalScopes(['only', 'sort', 'relations']);
};
}
return $relations;
}
/**
* Get operator and value prepared
*
* @param string $operator
* @param string $value
* @return array
*/
private static function prepareOperatorValue(string $operator, string $value): array
{
if (in_array($operator, ['like', 'ilike']))
$value = "%$value%";
return compact('operator', 'value');
}
/**
* Get model name without namespace
*
* @return string
*/
public static function getModelName(): string
{
return class_basename(get_called_class());
}
/**
* Get table name by the model name
*
* @return string
*/
public static function getTableName(): string
{
return Str::plural(Str::snake(static::getModelName()));
}
}