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

Sorts   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A filled() 0 3 1
A each() 0 3 1
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