1
|
|
|
<?php |
2
|
|
|
namespace Germania\Pagination; |
3
|
|
|
|
4
|
|
|
class Pagination implements PaginationInterface |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The total number of items to paginate |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
public $items_count; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The current page number |
16
|
|
|
* @var int|null |
17
|
|
|
*/ |
18
|
|
|
public $page_number; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The number of page items in use |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
public $page_size; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The default number of items on a page |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $default_page_size = 25; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The maximum page length |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $max_page_size = 100; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int $items_count The total number of items to paginate |
43
|
|
|
* @param int $page_size Optional: number of items on a single page, default: 25 |
44
|
|
|
* @param int $page_size Optional: Maximum number of items on a single page, default: 100 |
45
|
|
|
*/ |
46
|
144 |
|
public function __construct( int $items_count, int $page_size = null, int $max_page_size = null ) |
47
|
|
|
{ |
48
|
144 |
|
$this->items_count = $items_count; |
49
|
144 |
|
$this->page_size = $page_size ?: $this->default_page_size; |
50
|
144 |
|
$this->default_page_size = $this->page_size; |
51
|
144 |
|
$this->max_page_size = $max_page_size ?: $this->max_page_size; |
52
|
144 |
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
72 |
|
public function isActive() : bool |
59
|
|
|
{ |
60
|
72 |
|
return ($this->getCurrent() !== null); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
36 |
|
public function isDefaultPageSize() : bool |
68
|
|
|
{ |
69
|
36 |
|
return $this->getPageSize() === $this->default_page_size; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
36 |
|
public function getPagesCount() : int |
80
|
|
|
{ |
81
|
36 |
|
return ceil( $this->items_count / $this->getPageSize( )); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
108 |
|
public function getPageSize() |
89
|
|
|
{ |
90
|
108 |
|
return $this->page_size; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
* @throws PaginationRangeException When page size not between 1 and $max_page_size |
97
|
|
|
*/ |
98
|
28 |
|
public function setPageSize( $size ) |
99
|
|
|
{ |
100
|
|
|
$filter_options = array("options" => [ |
101
|
28 |
|
"min_range" => 1, |
102
|
28 |
|
"max_range" => $this->max_page_size |
103
|
|
|
]); |
104
|
|
|
|
105
|
28 |
View Code Duplication |
if (filter_var($size, FILTER_VALIDATE_INT, $filter_options) === false): |
|
|
|
|
106
|
8 |
|
$msg = sprintf("Invalid Page size (max. %s)", $this->max_page_size); |
107
|
8 |
|
throw new PaginationRangeException($msg, 400); |
108
|
|
|
endif; |
109
|
|
|
|
110
|
20 |
|
$this->page_size = $size; |
111
|
20 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
72 |
|
public function getCurrent() |
122
|
|
|
{ |
123
|
72 |
|
return $this->page_number; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritDoc |
130
|
|
|
* @throws PaginationInvalidArgumentException when page number is not integer |
131
|
|
|
* @throws PaginationRangeException when page number does not exists |
132
|
|
|
*/ |
133
|
96 |
|
public function setCurrent( $number ) |
134
|
|
|
{ |
135
|
96 |
|
$min_page_number = $this->getFirst(); |
136
|
96 |
|
$max_page_number = $this->getLast(); |
137
|
|
|
|
138
|
96 |
|
if (filter_var($number, FILTER_VALIDATE_INT) === false): |
139
|
4 |
|
$msg = sprintf("Integer (%s to %s) expected", $min_page_number, $max_page_number); |
140
|
4 |
|
throw new PaginationInvalidArgumentException($msg); |
141
|
|
|
endif; |
142
|
|
|
|
143
|
|
|
$filter_options = array("options" => [ |
144
|
92 |
|
"min_range" => $min_page_number, |
145
|
92 |
|
"max_range" => $max_page_number |
146
|
|
|
]); |
147
|
|
|
|
148
|
92 |
View Code Duplication |
if (filter_var($number, FILTER_VALIDATE_INT, $filter_options) === false): |
|
|
|
|
149
|
4 |
|
$msg = sprintf("Invalid Page number (allowed: %s to %s)", $min_page_number, $max_page_number); |
150
|
4 |
|
throw new PaginationRangeException($msg, 400); |
151
|
|
|
endif; |
152
|
|
|
|
153
|
88 |
|
$this->page_number = $number; |
154
|
88 |
|
return $this; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
48 |
View Code Duplication |
public function getPrevious() |
|
|
|
|
166
|
|
|
{ |
167
|
48 |
|
$page_number = $this->getCurrent(); |
168
|
48 |
|
if (!$this->isActive()): |
169
|
12 |
|
return null; |
170
|
|
|
endif; |
171
|
|
|
|
172
|
36 |
|
return $page_number > $this->getFirst() |
173
|
12 |
|
? $page_number - 1 |
174
|
36 |
|
: null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
48 |
View Code Duplication |
public function getNext() |
|
|
|
|
183
|
|
|
{ |
184
|
48 |
|
$page_number = $this->getCurrent(); |
185
|
48 |
|
if (!$this->isActive()): |
186
|
12 |
|
return null; |
187
|
|
|
endif; |
188
|
|
|
|
189
|
36 |
|
return $page_number < $this->getLast() |
190
|
36 |
|
? $page_number + 1 |
191
|
36 |
|
: null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritDoc |
199
|
|
|
*/ |
200
|
108 |
|
public function getFirst() : int |
201
|
|
|
{ |
202
|
108 |
|
return 0; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritDoc |
207
|
|
|
*/ |
208
|
108 |
|
public function getLast() : int |
209
|
|
|
{ |
210
|
108 |
|
return ceil( $this->items_count / $this->getPageSize( )) - 1; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.