1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Compolomus\Pagination; |
4
|
|
|
|
5
|
|
|
class Pagination |
6
|
|
|
{ |
7
|
|
|
private $total; |
8
|
|
|
|
9
|
|
|
private $page; |
10
|
|
|
|
11
|
|
|
private $limit; |
12
|
|
|
|
13
|
|
|
private $totalPages; |
14
|
|
|
|
15
|
|
|
private $length; |
16
|
|
|
|
17
|
|
|
private $pos; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Pagination constructor. |
21
|
|
|
* |
22
|
|
|
* @param int $page |
23
|
|
|
* @param int $limit |
24
|
|
|
* @param int $total |
25
|
|
|
* @param int $length |
26
|
|
|
*/ |
27
|
5 |
|
public function __construct(int $page, int $limit, int $total, int $length = 3, bool $uiKeys = true) |
28
|
|
|
{ |
29
|
5 |
|
$this->totalPages = (int)ceil($total / $limit); |
30
|
5 |
|
$this->page = $page > 1 ? ($page > $this->totalPages ? 1 : $page) : 1; |
31
|
5 |
|
$this->limit = $limit > 0 ? $limit : 10; |
32
|
5 |
|
$this->total = $total; |
33
|
5 |
|
$this->length = $length >= 0 ? $length : 3; |
34
|
5 |
|
$this->pos = $this->init(); |
35
|
5 |
|
$this->uiKeys = $uiKeys; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
private function init(): string |
39
|
|
|
{ |
40
|
5 |
|
$pos = 'full'; |
41
|
|
|
|
42
|
5 |
|
switch ($this->page) { |
43
|
5 |
|
case (($this->page - $this->length) < 3): |
44
|
5 |
|
$pos = 'noLeftDots'; |
45
|
5 |
|
break; |
46
|
1 |
|
case (($this->page - $this->length) >= 3 && ($this->totalPages - $this->page - $this->length) > 1): |
47
|
1 |
|
$pos = 'center'; |
48
|
1 |
|
break; |
49
|
1 |
|
case (abs($this->totalPages - $this->page - $this->length) >= 0): |
50
|
1 |
|
$pos = 'noRightDots'; |
51
|
1 |
|
break; |
52
|
|
|
case ($this->totalPages < 7): |
53
|
|
|
$pos = 'full'; |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
return $pos; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
1 |
|
public function getLimit(): int |
64
|
|
|
{ |
65
|
1 |
|
return $this->limit; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
1 |
|
public function getEnd(): int |
72
|
|
|
{ |
73
|
1 |
|
return $this->page === $this->totalPages ? $this->total : $this->page * $this->limit; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
1 |
|
public function getOffset(): int |
80
|
|
|
{ |
81
|
1 |
|
return $this->page === 1 ? 0 : ($this->page - 1) * $this->limit; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
1 |
|
private function start(): array |
88
|
|
|
{ |
89
|
1 |
|
$result = []; |
90
|
|
|
|
91
|
1 |
|
if ($this->page > 1) { |
92
|
1 |
|
$result['prev'] = $this->page - 1; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
if ($this->page !== 1) { |
96
|
1 |
|
$result['first'] = 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
if ($this->page > 3) { |
100
|
1 |
|
$result['second'] = 2; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $this->uiKeys ? $result : []; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
1 |
|
private function end(): array |
111
|
|
|
{ |
112
|
1 |
|
$result = []; |
113
|
|
|
|
114
|
1 |
|
if ($this->page !== $this->totalPages) { |
115
|
1 |
|
$result['last'] = $this->totalPages; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
if ($this->totalPages - $this->page > 0) { |
119
|
1 |
|
$result['next'] = $this->page + 1; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
if ($this->totalPages - $this->page + $this->length > 3) { |
123
|
1 |
|
$result['preLast'] = $this->totalPages - 1; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $this->uiKeys ? $result : []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
1 |
|
private function leftPad(): array |
133
|
|
|
{ |
134
|
1 |
|
$result = []; |
135
|
1 |
|
$for = []; |
136
|
|
|
|
137
|
1 |
|
if ($this->page - $this->length > 1) { |
138
|
1 |
|
$result[] = 1; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
if ($this->pos !== 'noLeftDots') { |
142
|
1 |
|
if ($this->uiKeys) { |
143
|
1 |
|
$result['leftDots'] = '...'; |
144
|
|
|
} else { |
145
|
|
|
$result[] = '...'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
foreach (range($this->page - 1, $this->page - $this->length) as $value) { |
150
|
1 |
|
if ($value > 0) { |
151
|
1 |
|
$for[] = $value; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return array_merge($result, array_reverse($for)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
1 |
|
private function rightPad(): array |
162
|
|
|
{ |
163
|
1 |
|
$result = []; |
164
|
|
|
|
165
|
1 |
|
foreach (range($this->page + 1, $this->page + $this->length) as $value) { |
166
|
1 |
|
if ($value <= $this->totalPages) { |
167
|
1 |
|
$result[] = $value; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
if ($this->pos !== 'noRightDots') { |
172
|
1 |
|
if ($this->uiKeys) { |
173
|
1 |
|
$result['rightDots'] = '...'; |
174
|
|
|
} else { |
175
|
|
|
$result[] = '...'; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
if ($this->totalPages - $this->page - $this->length > 0) { |
180
|
1 |
|
$result[] = $this->totalPages; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
private function getCurrent(): array |
187
|
|
|
{ |
188
|
1 |
|
if ($this->uiKeys) { |
189
|
1 |
|
$result['current'] = $this->page; |
|
|
|
|
190
|
|
|
} else { |
191
|
|
|
$result[] = $this->page; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return $result; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
1 |
|
public function get(): array |
201
|
|
|
{ |
202
|
1 |
|
return array_merge($this->start(), $this->leftPad(), $this->getCurrent(), $this->rightPad(), $this->end()); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|