1 | <?php |
||
10 | trait SortsQuery |
||
11 | { |
||
12 | /** @var \Illuminate\Support\Collection */ |
||
13 | protected $defaultSorts; |
||
14 | |||
15 | /** @var \Illuminate\Support\Collection */ |
||
16 | protected $allowedSorts; |
||
17 | |||
18 | /** @var bool */ |
||
19 | protected $sortsWereParsed = false; |
||
20 | |||
21 | public function allowedSorts($sorts): self |
||
22 | { |
||
23 | $sorts = is_array($sorts) ? $sorts : func_get_args(); |
||
24 | |||
25 | if (! $this->request->sorts()) { |
||
|
|||
26 | return $this; |
||
27 | } |
||
28 | |||
29 | $this->allowedSorts = collect($sorts)->map(function ($sort) { |
||
30 | if ($sort instanceof Sort) { |
||
31 | return $sort; |
||
32 | } |
||
33 | |||
34 | return Sort::field(ltrim($sort, '-')); |
||
35 | }); |
||
36 | |||
37 | $this->guardAgainstUnknownSorts(); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param array|string|\Spatie\QueryBuilder\Sort $sorts |
||
44 | * |
||
45 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
46 | */ |
||
47 | public function defaultSort($sorts): self |
||
51 | |||
52 | /** |
||
53 | * @param array|string|\Spatie\QueryBuilder\Sort $sorts |
||
54 | * |
||
55 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
56 | */ |
||
57 | public function defaultSorts($sorts): self |
||
71 | |||
72 | protected function parseSorts() |
||
108 | |||
109 | protected function findSort(string $property): ?Sort |
||
117 | |||
118 | protected function addDefaultSorts() |
||
131 | |||
132 | protected function guardAgainstUnknownSorts() |
||
146 | |||
147 | protected function filterDuplicates(Collection $sorts): Collection |
||
165 | } |
||
166 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: