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

Sorts::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
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 1
cts 1
cp 1
crap 1
rs 10
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