Sorts::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 1
b 0
f 1
cc 3
nc 2
nop 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) {
0 ignored issues
show
Bug introduced by
$params of type array|string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        $this->fields = collect(/** @scrutinizer ignore-type */ $params)->map(function ($field) {
Loading history...
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