sarala-io /
sarala-laravel
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Sarala\Query; |
||
| 6 | |||
| 7 | use Illuminate\Http\Request; |
||
| 8 | use Illuminate\Support\Collection; |
||
| 9 | use Illuminate\Support\Str; |
||
| 10 | |||
| 11 | class Sorts |
||
| 12 | { |
||
| 13 | /** @var Collection */ |
||
| 14 | private $fields; |
||
| 15 | |||
| 16 | public function __construct(Request $request) |
||
| 17 | { |
||
| 18 | $params = $request->filled('sort') ? explode(',', $request->get('sort')) : []; |
||
| 19 | |||
| 20 | $this->fields = collect($params)->map(function ($field) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 21 | $direction = SortField::SORT_ASCENDING; |
||
| 22 | |||
| 23 | if (Str::startsWith($field, '-')) { |
||
| 24 | $direction = SortField::SORT_DESCENDING; |
||
| 25 | $field = Str::after($field, '-'); |
||
| 26 | } |
||
| 27 | |||
| 28 | return new SortField($field, $direction); |
||
| 29 | }); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function each(callable $callback) |
||
| 33 | { |
||
| 34 | $this->fields->each($callback); |
||
| 35 | } |
||
| 36 | } |
||
| 37 |