Passed
Push — master ( 89df4c...863b29 )
by Milroy
12:00
created

Sorts   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 26
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getFields() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiChef\RequestQueryHelper;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
10
class Sorts
11
{
12
    private Collection $fields;
13
14
    public function __construct(string $sorts = null)
15
    {
16 6
        $this->fields = Collection::make();
17
        $sorts = (new QueryParamBag($sorts))->getParams();
18 6
        $fields = array_keys($sorts);
19
20 6
        collect($fields)->each(function (string $field) use ($sorts) {
21
            $params = $sorts[$field];
22
            $direction = SortField::DIRECTION_ASCENDING;
23 6
24
            if (Str::startsWith($field, '-')) {
25 6
                $direction = SortField::DIRECTION_DESCENDING;
26 6
                $field = Str::after($field, '-');
27 6
            }
28
29
            $this->fields->push(new SortField($field, $direction, $params));
30 6
        });
31 6
    }
32 6
33
    public function getFields(): Collection
34 3
    {
35
        return $this->fields;
36 3
    }
37
}
38