Passed
Pull Request — master (#65)
by Zing
05:54
created

Sort::asc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Zing\QueryBuilder\Sorts\SortField;
9
10
class Sort
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $property;
16
17
    /**
18
     * @var \Zing\QueryBuilder\Contracts\Sort
19
     */
20
    protected $sort;
21
22
    protected $column;
23
24
    protected $defaultDirection;
25
26
    /**
27
     * Sort constructor.
28
     *
29
     * @param string $property
30
     * @param string $column
31
     */
32 3
    public function __construct(string $property, Contracts\Sort $sort, $column)
33
    {
34 3
        $this->property = $property;
35 3
        $this->sort = $sort;
36 3
        $this->column = $column ?? $property;
37 3
    }
38
39 3
    public static function field(string $property, $column = null): self
40
    {
41 3
        return new static($property, new SortField(), $column);
42
    }
43
44 3
    public function getProperty(): string
45
    {
46 3
        return $this->property;
47
    }
48
49
    public function isForProperty(string $property): bool
50
    {
51
        return $this->property === $property;
52
    }
53
54
    public function getColumn(): string
55
    {
56
        return $this->column;
57
    }
58
59 1
    public function asc()
60
    {
61 1
        $this->defaultDirection = 'asc';
62
63 1
        return $this;
64
    }
65
66 1
    public function desc()
67
    {
68 1
        $this->defaultDirection = 'desc';
69
70 1
        return $this;
71
    }
72
73 1
    public function hasDefaultDirection(): bool
74
    {
75 1
        return $this->defaultDirection !== null;
76
    }
77
78 1
    public function getDefaultDirection()
79
    {
80 1
        return $this->defaultDirection;
81
    }
82
83 3
    public function sort($query, $direction): Builder
84
    {
85 3
        return $this->sort->apply($query, $direction === 'desc', $this->column);
86
    }
87
}
88