|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mav3rick177\RapidPagination\Base; |
|
4
|
|
|
|
|
5
|
|
|
use mav3rick177\RapidPagination\Base\Contracts\Cursor; |
|
6
|
|
|
use mav3rick177\RapidPagination\Base\Exceptions\InvalidArgumentException; |
|
7
|
|
|
use mav3rick177\RapidPagination\Base\Exceptions\Query\InsufficientConstraintsException; |
|
8
|
|
|
use mav3rick177\RapidPagination\Base\Query\Direction; |
|
9
|
|
|
use mav3rick177\RapidPagination\Base\Query\Limit; |
|
10
|
|
|
use mav3rick177\RapidPagination\Base\Query\Order; |
|
11
|
|
|
use mav3rick177\RapidPagination\Base\Query\Select; |
|
12
|
|
|
use mav3rick177\RapidPagination\Base\Query\SelectOrUnionAll; |
|
13
|
|
|
use mav3rick177\RapidPagination\Base\Query\UnionAll; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Query |
|
17
|
|
|
*/ |
|
18
|
|
|
class Query |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Select|SelectOrUnionAll|UnionAll |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $selectOrUnionAll; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Order[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $orders; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Cursor |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $cursor; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $limit; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Direction |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $direction; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $exclusive; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $seekable; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $builder; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string[][] $orders |
|
62
|
|
|
* @param Cursor|int[]|string[] $cursor |
|
63
|
|
|
* @param int $limit |
|
64
|
|
|
* @param bool $backward |
|
65
|
|
|
* @param bool $exclusive |
|
66
|
|
|
* @param bool $seekable |
|
67
|
|
|
* @param mixed $builder |
|
68
|
|
|
* @return static |
|
69
|
|
|
*/ |
|
70
|
17 |
|
public static function create(array $orders, $cursor, $limit, $backward, $exclusive, $seekable, $builder = null) |
|
71
|
|
|
{ |
|
72
|
17 |
|
if (!$cursor instanceof Cursor && !is_array($cursor)) { |
|
73
|
|
|
throw new InvalidArgumentException('Cursor must be either Cursor or array'); |
|
74
|
|
|
} |
|
75
|
17 |
|
if (!is_bool($backward)) { |
|
76
|
|
|
throw new InvalidArgumentException('Direction must be bool'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
17 |
|
$orders = Order::createMany($orders); |
|
80
|
17 |
|
$cursor = $cursor instanceof Cursor ? $cursor : new ArrayCursor($cursor); |
|
81
|
17 |
|
$limit = new Limit($limit); |
|
82
|
17 |
|
$direction = new Direction($backward ? Direction::BACKWARD : Direction::FORWARD); |
|
83
|
17 |
|
$selectOrUnionAll = SelectOrUnionAll::create($orders, $cursor, $limit, $direction, $exclusive, $seekable); |
|
84
|
17 |
|
return new static($selectOrUnionAll, $orders, $cursor, $limit, $direction, $exclusive, $seekable, $builder); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Query constructor. |
|
89
|
|
|
* |
|
90
|
|
|
* @param Select|SelectOrUnionAll|UnionAll $selectOrUnionAll |
|
91
|
|
|
* @param Order[] $orders |
|
92
|
|
|
* @param Cursor $cursor |
|
93
|
|
|
* @param Limit $limit |
|
94
|
|
|
* @param Direction $direction |
|
95
|
|
|
* @param bool $exclusive |
|
96
|
|
|
* @param bool $seekable |
|
97
|
|
|
* @param mixed $builder |
|
98
|
|
|
*/ |
|
99
|
17 |
|
public function __construct(SelectOrUnionAll $selectOrUnionAll, array $orders, Cursor $cursor, Limit $limit, Direction $direction, $exclusive, $seekable, $builder = null) |
|
100
|
|
|
{ |
|
101
|
17 |
|
if (!$orders) { |
|
|
|
|
|
|
102
|
|
|
throw new InsufficientConstraintsException('At least one order constraint required'); |
|
103
|
|
|
} |
|
104
|
17 |
|
if (!is_bool($exclusive)) { |
|
105
|
|
|
throw new InvalidArgumentException('Exclusive must be bool'); |
|
106
|
|
|
} |
|
107
|
17 |
|
if (!is_bool($seekable)) { |
|
108
|
|
|
throw new InvalidArgumentException('Seekable must be bool'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
17 |
|
$this->selectOrUnionAll = $selectOrUnionAll; |
|
112
|
17 |
|
$this->orders = $orders; |
|
113
|
17 |
|
$this->cursor = $cursor; |
|
114
|
17 |
|
$this->limit = $limit->original(); |
|
115
|
17 |
|
$this->direction = $direction; |
|
116
|
17 |
|
$this->exclusive = $exclusive; |
|
117
|
17 |
|
$this->seekable = $seekable; |
|
118
|
17 |
|
$this->builder = $builder; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return Select|SelectOrUnionAll|UnionAll |
|
123
|
|
|
*/ |
|
124
|
17 |
|
public function selectOrUnionAll() |
|
125
|
|
|
{ |
|
126
|
17 |
|
return $this->selectOrUnionAll; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return Order[] |
|
131
|
|
|
*/ |
|
132
|
|
|
public function orders() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->orders; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return Cursor |
|
139
|
|
|
*/ |
|
140
|
|
|
public function cursor() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->cursor; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return int |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function limit() |
|
149
|
|
|
{ |
|
150
|
2 |
|
return $this->limit; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return Direction |
|
155
|
|
|
*/ |
|
156
|
2 |
|
public function direction() |
|
157
|
|
|
{ |
|
158
|
2 |
|
return $this->direction; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function forward() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->direction()->forward(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
|
|
public function backward() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->direction()->backward(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
public function exclusive() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->exclusive; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function inclusive() |
|
189
|
|
|
{ |
|
190
|
|
|
return !$this->exclusive; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
|
|
public function seekable() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->seekable; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function unseekable() |
|
205
|
|
|
{ |
|
206
|
|
|
return !$this->seekable; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return mixed |
|
211
|
|
|
*/ |
|
212
|
2 |
|
public function builder() |
|
213
|
|
|
{ |
|
214
|
2 |
|
return $this->builder; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Clone Query. |
|
219
|
|
|
*/ |
|
220
|
|
View Code Duplication |
public function __clone() |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
$this->selectOrUnionAll = clone $this->selectOrUnionAll; |
|
223
|
|
|
$this->orders = array_map(static function (Order $order) { |
|
224
|
|
|
return clone $order; |
|
225
|
|
|
}, $this->orders); |
|
226
|
|
|
$this->direction = clone $this->direction; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.