Completed
Push — master ( 870cb1...1f9632 )
by Milroy
07:07 queued 05:53
created

Sorts::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 2
nop 1
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) {
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