1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Source; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-core' |
8
|
|
|
use Charcoal\Source\AbstractExpression; |
9
|
|
|
use Charcoal\Source\PaginationInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Pagination Clause |
13
|
|
|
* |
14
|
|
|
* For limiting the results of a query. |
15
|
|
|
*/ |
16
|
|
|
class Pagination extends AbstractExpression implements |
17
|
|
|
PaginationInterface |
18
|
|
|
{ |
19
|
|
|
const DEFAULT_PAGE = 1; |
20
|
|
|
const DEFAULT_COUNT = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The current page. |
24
|
|
|
* |
25
|
|
|
* @var integer |
26
|
|
|
*/ |
27
|
|
|
protected $page = self::DEFAULT_PAGE; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The number of results per page. |
31
|
|
|
* |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
protected $numPerPage = self::DEFAULT_COUNT; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the pagination clause data. |
38
|
|
|
* |
39
|
|
|
* @param array<string,mixed> $data The expression data; |
40
|
|
|
* as an associative array. |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
|
|
public function setData(array $data) |
44
|
|
|
{ |
45
|
|
|
parent::setData($data); |
46
|
|
|
|
47
|
|
|
if (isset($data['page'])) { |
48
|
|
|
$this->setPage($data['page']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (isset($data['per_page'])) { |
52
|
|
|
$this->setNumPerPage($data['per_page']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (isset($data['num_per_page'])) { |
56
|
|
|
$this->setNumPerPage($data['num_per_page']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve the default values for pagination. |
64
|
|
|
* |
65
|
|
|
* @return array<string,mixed> An associative array. |
66
|
|
|
*/ |
67
|
|
|
public function defaultData() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'page' => self::DEFAULT_PAGE, |
71
|
|
|
'num_per_page' => self::DEFAULT_COUNT, |
72
|
|
|
'active' => true, |
73
|
|
|
'name' => null, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve the pagination clause structure. |
79
|
|
|
* |
80
|
|
|
* @return array<string,mixed> An associative array. |
81
|
|
|
*/ |
82
|
|
|
public function data() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'page' => $this->page(), |
86
|
|
|
'num_per_page' => $this->numPerPage(), |
87
|
|
|
'active' => $this->active(), |
88
|
|
|
'name' => $this->name(), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the page number. |
94
|
|
|
* |
95
|
|
|
* @param integer $page The current page. |
96
|
|
|
* Pages should start at 1. |
97
|
|
|
* @throws InvalidArgumentException If the parameter is not numeric or < 0. |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
|
|
public function setPage($page) |
101
|
|
|
{ |
102
|
|
|
if (!is_numeric($page)) { |
|
|
|
|
103
|
|
|
throw new InvalidArgumentException( |
104
|
|
|
'Page number must be numeric.' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$page = (int)$page; |
109
|
|
|
if ($page === 0) { |
110
|
|
|
$page = 1; |
111
|
|
|
} elseif ($page < 0) { |
112
|
|
|
throw new InvalidArgumentException( |
113
|
|
|
'Page number must be greater than zero.' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->page = $page; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve the page number. |
123
|
|
|
* |
124
|
|
|
* @return integer |
125
|
|
|
*/ |
126
|
|
|
public function page() |
127
|
|
|
{ |
128
|
|
|
return $this->page; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the number of results per page. |
133
|
|
|
* |
134
|
|
|
* @param integer $count The number of results to return, per page. |
135
|
|
|
* Use 0 to request all results. |
136
|
|
|
* @throws InvalidArgumentException If the parameter is not numeric or < 0. |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
|
|
public function setNumPerPage($count) |
140
|
|
|
{ |
141
|
|
|
if (!is_numeric($count)) { |
|
|
|
|
142
|
|
|
throw new InvalidArgumentException( |
143
|
|
|
'Number Per Page must be numeric.' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$count = (int)$count; |
148
|
|
|
if ($count < 0) { |
149
|
|
|
throw new InvalidArgumentException( |
150
|
|
|
'Number Per Page must be greater than zero.' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->numPerPage = $count; |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Retrieve the number of results per page. |
160
|
|
|
* |
161
|
|
|
* @return integer |
162
|
|
|
*/ |
163
|
|
|
public function numPerPage() |
164
|
|
|
{ |
165
|
|
|
return $this->numPerPage; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Retrieve the pagination's lowest possible index. |
170
|
|
|
* |
171
|
|
|
* @return integer |
172
|
|
|
*/ |
173
|
|
|
public function first() |
174
|
|
|
{ |
175
|
|
|
$page = $this->page(); |
176
|
|
|
$limit = $this->numPerPage(); |
177
|
|
|
|
178
|
|
|
return max(0, (($page - 1) * $limit)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retrieve the pagination's highest possible index. |
183
|
|
|
* |
184
|
|
|
* Note: Can be greater than the actual number of items in storage. |
185
|
|
|
* |
186
|
|
|
* @return integer |
187
|
|
|
*/ |
188
|
|
|
public function last() |
189
|
|
|
{ |
190
|
|
|
$first = $this->first(); |
191
|
|
|
$limit = $this->numPerPage(); |
192
|
|
|
|
193
|
|
|
return ($first + $limit); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|