Passed
Pull Request — master (#96)
by Zing
04:26
created

Sort::hasDefaultDirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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
    /**
25
     * @var string
26
     */
27
    protected $defaultDirection;
28
29
    /**
30
     * Sort constructor.
31
     *
32
     * @param string $property
33
     * @param string $column
34
     */
35 4
    public function __construct(string $property, Contracts\Sort $sort, $column)
36
    {
37 4
        $this->property = $property;
38 4
        $this->sort = $sort;
39 4
        $this->column = $column ?? $property;
40 4
    }
41
42 4
    public static function field(string $property, $column = null): self
43
    {
44 4
        return new static($property, new SortField(), $column);
45
    }
46
47 3
    public function getProperty(): string
48
    {
49 3
        return $this->property;
50
    }
51
52 1
    public function isForProperty(string $property): bool
53
    {
54 1
        return $this->property === $property;
55
    }
56
57 1
    public function getColumn(): string
58
    {
59 1
        return $this->column;
60
    }
61
62 1
    public function asc(): self
63
    {
64 1
        $this->defaultDirection = 'asc';
65
66 1
        return $this;
67
    }
68
69 1
    public function desc(): self
70
    {
71 1
        $this->defaultDirection = 'desc';
72
73 1
        return $this;
74
    }
75
76 1
    public function hasDefaultDirection(): bool
77
    {
78 1
        return $this->defaultDirection !== null;
79
    }
80
81 1
    public function getDefaultDirection(): string
82
    {
83 1
        return $this->defaultDirection;
84
    }
85
86 3
    public function sort($query, $direction): Builder
87
    {
88 3
        return $this->sort->apply($query, $direction === 'desc', $this->column);
89
    }
90
}
91