1 | <?php |
||
11 | class QueryBuilder extends Builder |
||
12 | { |
||
13 | /** @var \Illuminate\Support\Collection */ |
||
14 | protected $allowedFilters; |
||
15 | |||
16 | /** @var string|null */ |
||
17 | protected $defaultSort; |
||
18 | |||
19 | /** @var \Illuminate\Support\Collection */ |
||
20 | protected $allowedSorts; |
||
21 | |||
22 | /** @var \Illuminate\Support\Collection */ |
||
23 | protected $allowedIncludes; |
||
24 | |||
25 | /** @var \Illuminate\Http\Request */ |
||
26 | protected $request; |
||
27 | |||
28 | public function __construct(Builder $builder, ?Request $request = null) |
||
44 | |||
45 | /** |
||
46 | * Create a new QueryBuilder for a request and model. |
||
47 | * |
||
48 | * @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
||
49 | * @param Request $request |
||
50 | * |
||
51 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
52 | */ |
||
53 | public static function for($baseQuery, ?Request $request = null): self |
||
61 | |||
62 | public function allowedFilters($filters): self |
||
63 | { |
||
64 | $filters = is_array($filters) ? $filters : func_get_args(); |
||
65 | $this->allowedFilters = collect($filters)->map(function ($filter) { |
||
66 | if ($filter instanceof Filter) { |
||
67 | return $filter; |
||
68 | } |
||
69 | |||
70 | return Filter::partial($filter); |
||
71 | }); |
||
72 | |||
73 | $this->guardAgainstUnknownFilters(); |
||
74 | |||
75 | $this->addFiltersToQuery($this->request->filters()); |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | public function defaultSort($sort): self |
||
81 | { |
||
82 | $this->defaultSort = $sort; |
||
83 | |||
84 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
85 | |||
86 | return $this; |
||
87 | } |
||
88 | |||
89 | public function allowedSorts($sorts): self |
||
90 | { |
||
91 | $sorts = is_array($sorts) ? $sorts : func_get_args(); |
||
92 | if (! $this->request->sorts()) { |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | $this->allowedSorts = collect($sorts); |
||
97 | |||
98 | if (! $this->allowedSorts->contains('*')) { |
||
99 | $this->guardAgainstUnknownSorts(); |
||
100 | } |
||
101 | |||
102 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function allowedIncludes($includes): self |
||
108 | { |
||
109 | $includes = is_array($includes) ? $includes : func_get_args(); |
||
110 | $this->allowedIncludes = collect($includes); |
||
111 | |||
112 | $this->guardAgainstUnknownIncludes(); |
||
113 | |||
114 | $this->addIncludesToQuery($this->request->includes()); |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | protected function addFiltersToQuery(Collection $filters) |
||
127 | |||
128 | protected function findFilter(string $property) : ?Filter |
||
135 | |||
136 | protected function addSortsToQuery(Collection $sorts) |
||
137 | { |
||
138 | $sorts |
||
139 | ->each(function (string $sort) { |
||
147 | |||
148 | protected function addIncludesToQuery(Collection $includes) |
||
158 | |||
159 | protected function guardAgainstUnknownFilters() |
||
171 | |||
172 | protected function guardAgainstUnknownSorts() |
||
184 | |||
185 | protected function guardAgainstUnknownIncludes() |
||
195 | } |
||
196 |