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