|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Hydrogen package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Query; |
|
11
|
|
|
|
|
12
|
|
|
use RDS\Hydrogen\Criteria\OrderBy; |
|
13
|
|
|
use RDS\Hydrogen\Criteria\Where\Operator; |
|
14
|
|
|
use RDS\Hydrogen\Query; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Trait OrderProvider |
|
18
|
|
|
* @mixin Query |
|
19
|
|
|
*/ |
|
20
|
|
|
trait OrderProvider |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $field |
|
24
|
|
|
* @param mixed $value |
|
25
|
|
|
* @param bool $including |
|
26
|
|
|
* @return Query|$this|self |
|
27
|
|
|
*/ |
|
28
|
|
|
public function after(string $field, $value, bool $including = false): self |
|
29
|
|
|
{ |
|
30
|
|
|
$operator = $including ? Operator::GT : Operator::GTE; |
|
31
|
|
|
|
|
32
|
|
|
return $this->where($field, $operator, $value)->asc($field); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string ...$fields |
|
37
|
|
|
* @return Query|$this|self |
|
38
|
|
|
*/ |
|
39
|
|
|
public function asc(string ...$fields): self |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($fields as $field) { |
|
42
|
|
|
$this->orderBy($field); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $field |
|
50
|
|
|
* @param bool $asc |
|
51
|
|
|
* @return Query|$this|self |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function orderBy(string $field, bool $asc = true): self |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->add(new OrderBy($this, $field, $asc)); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $field |
|
60
|
|
|
* @param mixed $value |
|
61
|
|
|
* @param bool $including |
|
62
|
|
|
* @return Query|$this|self |
|
63
|
|
|
*/ |
|
64
|
|
|
public function before(string $field, $value, bool $including = false): self |
|
65
|
|
|
{ |
|
66
|
|
|
$operator = $including ? Operator::LT : Operator::LTE; |
|
67
|
|
|
|
|
68
|
|
|
return $this->where($field, $operator, $value)->desc($field); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string ...$fields |
|
73
|
|
|
* @return Query|$this|self |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function desc(string ...$fields): self |
|
76
|
|
|
{ |
|
77
|
2 |
|
foreach ($fields as $field) { |
|
78
|
2 |
|
$this->orderBy($field, false); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $field |
|
86
|
|
|
* @return Query|$this|self |
|
87
|
|
|
*/ |
|
88
|
|
|
public function latest(string $field = 'createdAt'): self |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->desc($field); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $field |
|
95
|
|
|
* @return Query|$this|self |
|
96
|
|
|
*/ |
|
97
|
|
|
public function oldest(string $field = 'createdAt'): self |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->asc($field); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.