Completed
Push — master ( b68b20...ae6559 )
by Milroy
02:48
created

Sorts::filled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiChef\RequestQueryHelper;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Str;
10
11
class Sorts
12
{
13
    /** @var Collection $fields */
14
    private $fields;
15
16 6
    public function __construct(Request $request)
17
    {
18 6
        $paramName = config('request-query-helper.sort');
19
20 6
        $params = $request->filled($paramName) ? explode(',', $request->get($paramName)) : [];
21
22
        $this->fields = collect($params)->map(function ($field) {
23 6
            $direction = SortField::DIRECTION_ASCENDING;
24
25 6
            if (Str::startsWith($field, '-')) {
26 6
                $direction = SortField::DIRECTION_DESCENDING;
27 6
                $field = Str::after($field, '-');
28
            }
29
30 6
            return new SortField($field, $direction);
31 6
        });
32 6
    }
33
34 3
    public function filled(): bool
35
    {
36 3
        return $this->fields->isNotEmpty();
37
    }
38
39 3
    public function each(callable $callback)
40
    {
41 3
        $this->fields->each($callback);
42 3
    }
43
}
44