Completed
Push — master ( 870cb1...1f9632 )
by Milroy
07:07 queued 05:53
created

Sorts   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A each() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Query;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Str;
10
11
class Sorts
12
{
13
    /** @var Collection */
14
    private $fields;
15
16
    public function __construct(Request $request)
17
    {
18
        $params = $request->filled('sort') ? explode(',', $request->get('sort')) : [];
19
20
        $this->fields = collect($params)->map(function ($field) {
21
            $direction = SortField::SORT_ASCENDING;
22
23
            if (Str::startsWith($field, '-')) {
24
                $direction = SortField::SORT_DESCENDING;
25
                $field = Str::after($field, '-');
26
            }
27
28
            return new SortField($field, $direction);
29
        });
30
    }
31
32
    public function each(callable $callback)
33
    {
34
        $this->fields->each($callback);
35
    }
36
}
37