|
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\FieldSort; |
|
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
|
|
|
/** |
|
23
|
|
|
* @var \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $column; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $defaultDirection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Sort constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string|null $column |
|
36
|
|
|
*/ |
|
37
|
7 |
|
public function __construct(string $property, Contracts\Sort $sort, $column) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->property = $property; |
|
40
|
7 |
|
$this->sort = $sort; |
|
41
|
7 |
|
$this->column = $column ?? $property; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string|null $column |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public static function field(string $property, $column = null): self |
|
48
|
|
|
{ |
|
49
|
7 |
|
return new self($property, new FieldSort(), $column); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
public function getProperty(): string |
|
53
|
|
|
{ |
|
54
|
6 |
|
return $this->property; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function isForProperty(string $property): bool |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->property === $property; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getColumn() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->column; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function asc(): self |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->defaultDirection = 'asc'; |
|
73
|
|
|
|
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function desc(): self |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->defaultDirection = 'desc'; |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function hasDefaultDirection(): bool |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->defaultDirection !== null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function getDefaultDirection(): string |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->defaultDirection; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
public function sort(Builder $query, string $direction): Builder |
|
95
|
|
|
{ |
|
96
|
6 |
|
return $this->sort->apply($query, $direction === 'desc', $this->column); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|