Completed
Push — master ( f994a3...fb5694 )
by
unknown
01:37
created

Sort::custom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Spatie\QueryBuilder\Sorts\SortsField;
7
use Spatie\QueryBuilder\Sorts\Sort as CustomSort;
8
9
class Sort
10
{
11
    /** @var string */
12
    protected $sortClass;
13
14
    /** @var string */
15
    protected $property;
16
17
    public function __construct(string $property, $sortClass)
18
    {
19
        $this->property = $property;
20
        $this->sortClass = $sortClass;
21
    }
22
23
    public function sort(Builder $builder, $descending)
24
    {
25
        $sortClass = $this->resolveSortClass();
26
27
        ($sortClass)($builder, $descending, $this->property);
28
    }
29
30
    public static function field(string $property) : self
31
    {
32
        return new static($property, SortsField::class);
33
    }
34
35
    public static function custom(string $property, $sortClass) : self
36
    {
37
        return new static($property, $sortClass);
38
    }
39
40
    public function getProperty(): string
41
    {
42
        return $this->property;
43
    }
44
45
    public function isForProperty(string $property): bool
46
    {
47
        return $this->property === $property;
48
    }
49
50
    private function resolveSortClass(): CustomSort
51
    {
52
        if ($this->sortClass instanceof CustomSort) {
53
            return $this->sortClass;
54
        }
55
56
        return new $this->sortClass;
57
    }
58
}
59