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

Sorts::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 2
rs 9.9332
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