1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Spatie\QueryBuilder\Exceptions\InvalidSortQuery; |
9
|
|
|
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery; |
10
|
|
|
use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery; |
11
|
|
|
use Spatie\QueryBuilder\Exceptions\InvalidIncludeQuery; |
12
|
|
|
|
13
|
|
|
class QueryBuilder extends Builder |
14
|
|
|
{ |
15
|
|
|
/** @var \Illuminate\Support\Collection */ |
16
|
|
|
protected $allowedFilters; |
17
|
|
|
|
18
|
|
|
/** @var string|null */ |
19
|
|
|
protected $defaultSort; |
20
|
|
|
|
21
|
|
|
/** @var \Illuminate\Support\Collection */ |
22
|
|
|
protected $allowedSorts; |
23
|
|
|
|
24
|
|
|
/** @var \Illuminate\Support\Collection */ |
25
|
|
|
protected $allowedIncludes; |
26
|
|
|
|
27
|
|
|
/** @var \Illuminate\Support\Collection */ |
28
|
|
|
protected $allowedAppends; |
29
|
|
|
|
30
|
|
|
/** @var \Illuminate\Support\Collection */ |
31
|
|
|
protected $fields; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $appends = []; |
35
|
|
|
|
36
|
|
|
/** @var \Illuminate\Http\Request */ |
37
|
|
|
protected $request; |
38
|
|
|
|
39
|
|
|
public function __construct(Builder $builder, ? Request $request = null) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(clone $builder->getQuery()); |
42
|
|
|
|
43
|
|
|
$this->initializeFromBuilder($builder); |
44
|
|
|
|
45
|
|
|
$this->request = $request ?? request(); |
46
|
|
|
|
47
|
|
|
$this->parseSelectedFields(); |
48
|
|
|
|
49
|
|
|
if ($this->request->sorts()) { |
50
|
|
|
$this->allowedSorts('*'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add the model, scopes, eager loaded relationships, local macro's and onDelete callback |
56
|
|
|
* from the $builder to this query builder. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
59
|
|
|
*/ |
60
|
|
|
protected function initializeFromBuilder(Builder $builder) |
61
|
|
|
{ |
62
|
|
|
$this->setModel($builder->getModel()) |
63
|
|
|
->setEagerLoads($builder->getEagerLoads()); |
64
|
|
|
|
65
|
|
|
$builder->macro('getProtected', function (Builder $builder, string $property) { |
66
|
|
|
return $builder->{$property}; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$this->scopes = $builder->getProtected('scopes'); |
70
|
|
|
|
71
|
|
|
$this->localMacros = $builder->getProtected('localMacros'); |
72
|
|
|
|
73
|
|
|
$this->onDelete = $builder->getProtected('onDelete'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create a new QueryBuilder for a request and model. |
78
|
|
|
* |
79
|
|
|
* @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
80
|
|
|
* @param Request $request |
81
|
|
|
* |
82
|
|
|
* @return \Spatie\QueryBuilder\QueryBuilder |
83
|
|
|
*/ |
84
|
|
|
public static function for($baseQuery, ? Request $request = null) : self |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (is_string($baseQuery)) { |
87
|
|
|
$baseQuery = ($baseQuery)::query(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new static($baseQuery, $request ?? request()); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function allowedFilters($filters) : self |
94
|
|
|
{ |
95
|
|
|
$filters = is_array($filters) ? $filters : func_get_args(); |
96
|
|
|
$this->allowedFilters = collect($filters)->map(function ($filter) { |
97
|
|
|
if ($filter instanceof Filter) { |
98
|
|
|
return $filter; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return Filter::partial($filter); |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
$this->guardAgainstUnknownFilters(); |
105
|
|
|
|
106
|
|
|
$this->addFiltersToQuery($this->request->filters()); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function defaultSort($sort) : self |
112
|
|
|
{ |
113
|
|
|
$this->defaultSort = $sort; |
114
|
|
|
|
115
|
|
|
$this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function allowedSorts($sorts) : self |
121
|
|
|
{ |
122
|
|
|
$sorts = is_array($sorts) ? $sorts : func_get_args(); |
123
|
|
|
if (! $this->request->sorts()) { |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->allowedSorts = collect($sorts); |
128
|
|
|
|
129
|
|
|
if (! $this->allowedSorts->contains('*')) { |
130
|
|
|
$this->guardAgainstUnknownSorts(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function allowedIncludes($includes) : self |
139
|
|
|
{ |
140
|
|
|
$includes = is_array($includes) ? $includes : func_get_args(); |
141
|
|
|
|
142
|
|
|
$this->allowedIncludes = collect($includes); |
143
|
|
|
|
144
|
|
|
$this->guardAgainstUnknownIncludes(); |
145
|
|
|
|
146
|
|
|
$this->addIncludesToQuery($this->request->includes()); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function allowedAppends($appends) : self |
152
|
|
|
{ |
153
|
|
|
$appends = is_array($appends) ? $appends : func_get_args(); |
154
|
|
|
|
155
|
|
|
$this->allowedAppends = collect($appends); |
156
|
|
|
|
157
|
|
|
$this->guardAgainstUnknownAppends(); |
158
|
|
|
|
159
|
|
|
$this->appends = $this->request->appends(); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function parseSelectedFields() |
165
|
|
|
{ |
166
|
|
|
$this->fields = $this->request->fields(); |
167
|
|
|
|
168
|
|
|
$modelFields = $this->fields->get( |
169
|
|
|
$this->getModel()->getTable() |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
if (! $modelFields) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->select(explode(',', $modelFields)); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function getFieldsForRelatedTable(string $relation): array |
180
|
|
|
{ |
181
|
|
|
$fields = $this->fields->get($relation); |
182
|
|
|
|
183
|
|
|
if (! $fields) { |
184
|
|
|
return []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return explode(',', $fields); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected function addFiltersToQuery(Collection $filters) |
191
|
|
|
{ |
192
|
|
|
$filters->each(function ($value, $property) { |
193
|
|
|
$filter = $this->findFilter($property); |
194
|
|
|
|
195
|
|
|
$filter->filter($this, $value); |
196
|
|
|
}); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function findFilter(string $property) : ? Filter |
200
|
|
|
{ |
201
|
|
|
return $this->allowedFilters |
202
|
|
|
->first(function (Filter $filter) use ($property) { |
203
|
|
|
return $filter->isForProperty($property); |
204
|
|
|
}); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
protected function addSortsToQuery(Collection $sorts) |
208
|
|
|
{ |
209
|
|
|
$sorts |
210
|
|
|
->each(function (string $sort) { |
211
|
|
|
$descending = $sort[0] === '-'; |
212
|
|
|
|
213
|
|
|
$key = ltrim($sort, '-'); |
214
|
|
|
|
215
|
|
|
$this->orderBy($key, $descending ? 'desc' : 'asc'); |
|
|
|
|
216
|
|
|
}); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function addIncludesToQuery(Collection $includes) |
220
|
|
|
{ |
221
|
|
|
$includes |
222
|
|
|
->map('camel_case') |
223
|
|
|
->map(function (string $include) { |
224
|
|
|
return collect(explode('.', $include)); |
225
|
|
|
}) |
226
|
|
|
->flatMap(function (Collection $relatedTables) { |
227
|
|
|
return $relatedTables |
228
|
|
|
->mapWithKeys(function ($table, $key) use ($relatedTables) { |
229
|
|
|
$fields = $this->getFieldsForRelatedTable(snake_case($table)); |
230
|
|
|
|
231
|
|
|
$fullRelationName = $relatedTables->slice(0, $key + 1)->implode('.'); |
232
|
|
|
|
233
|
|
|
if (empty($fields)) { |
234
|
|
|
return [$fullRelationName]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return [$fullRelationName => function ($query) use ($fields) { |
238
|
|
|
$query->select($fields); |
239
|
|
|
}]; |
240
|
|
|
}); |
241
|
|
|
}) |
242
|
|
|
->pipe(function (Collection $withs) { |
243
|
|
|
$this->with($withs->all()); |
244
|
|
|
}); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function setAppendsToResult($result) |
248
|
|
|
{ |
249
|
|
|
$result->map(function ($item) { |
250
|
|
|
$item->append($this->appends->toArray()); |
|
|
|
|
251
|
|
|
|
252
|
|
|
return $item; |
253
|
|
|
}); |
254
|
|
|
|
255
|
|
|
return $result; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
protected function guardAgainstUnknownFilters() |
259
|
|
|
{ |
260
|
|
|
$filterNames = $this->request->filters()->keys(); |
261
|
|
|
|
262
|
|
|
$allowedFilterNames = $this->allowedFilters->map->getProperty(); |
263
|
|
|
|
264
|
|
|
$diff = $filterNames->diff($allowedFilterNames); |
265
|
|
|
|
266
|
|
|
if ($diff->count()) { |
267
|
|
|
throw InvalidFilterQuery::filtersNotAllowed($diff, $allowedFilterNames); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
protected function guardAgainstUnknownSorts() |
272
|
|
|
{ |
273
|
|
|
$sorts = $this->request->sorts()->map(function ($sort) { |
274
|
|
|
return ltrim($sort, '-'); |
275
|
|
|
}); |
276
|
|
|
|
277
|
|
|
$diff = $sorts->diff($this->allowedSorts); |
278
|
|
|
|
279
|
|
|
if ($diff->count()) { |
280
|
|
|
throw InvalidSortQuery::sortsNotAllowed($diff, $this->allowedSorts); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
protected function guardAgainstUnknownIncludes() |
285
|
|
|
{ |
286
|
|
|
$includes = $this->request->includes(); |
287
|
|
|
|
288
|
|
|
$diff = $includes->diff($this->allowedIncludes); |
289
|
|
|
|
290
|
|
|
if ($diff->count()) { |
291
|
|
|
throw InvalidIncludeQuery::includesNotAllowed($diff, $this->allowedIncludes); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
protected function guardAgainstUnknownAppends() |
296
|
|
|
{ |
297
|
|
|
$appends = $this->request->appends(); |
298
|
|
|
|
299
|
|
|
$diff = $appends->diff($this->allowedAppends); |
300
|
|
|
|
301
|
|
|
if ($diff->count()) { |
302
|
|
|
throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function get($columns = ['*']) |
307
|
|
|
{ |
308
|
|
|
$result = parent::get($columns); |
309
|
|
|
|
310
|
|
|
if (count($this->appends) > 0) { |
311
|
|
|
$result = $this->setAppendsToResult($result); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $result; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|