-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagination.php
149 lines (119 loc) · 4.56 KB
/
Pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php declare(strict_types = 1);
/*
* This file is part of the Vairogs package.
*
* (c) Dāvis Zālītis (k0d3r1s) <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vairogs\Component\Functions;
use InvalidArgumentException;
use function ceil;
use function floor;
use function range;
use function sprintf;
final class Pagination
{
private const int MIN_VISIBLE = 5;
private int $current;
private int $indicator = -1;
private int $total;
private int $visible;
public function paginate(
int $visible,
int $total,
int $current,
int $indicator = -1,
): array {
$this->visible = $visible;
$this->total = $total;
$this->current = $current;
$this->indicator = $indicator;
$this->checkMinimum();
return $this->getPaginationData();
}
private function checkMinimum(): void
{
if ($this->visible < self::MIN_VISIBLE) {
throw new InvalidArgumentException(message: sprintf('Maximum of number of visible pages (%d) should be at least %d', $this->visible, self::MIN_VISIBLE));
}
}
private function getDataWithSingleOmitted(): array
{
$rest = $this->visible - ($this->total - $this->current);
$omitPagesFrom = (int) ceil(num: $rest / 2);
$omitPagesTo = $this->current - ($rest - $omitPagesFrom);
if ($this->hasSingleOmittedNearLast()) {
$rest = $this->visible - $this->current;
$omitPagesFrom = ((int) ceil(num: $rest / 2)) + $this->current;
$omitPagesTo = $this->total - ($this->visible - $omitPagesFrom);
}
return [
...range(start: 1, end: $omitPagesFrom - 1),
...[$this->indicator],
...range(start: $omitPagesTo + 1, end: $this->total),
];
}
private function getDataWithTwoOmitted(): array
{
$withoutCurrent = ($this->visible - 1) / 2;
$visibleLeft = floor(num: $withoutCurrent);
$visibleRight = ceil(num: $withoutCurrent);
if ($this->current <= ceil(num: $this->total / 2)) {
$visibleLeft = ceil(num: $withoutCurrent);
$visibleRight = floor(num: $withoutCurrent);
}
$omitLeftFrom = floor(num: $visibleLeft / 2) + 1;
$omitRightFrom = ceil(num: $visibleRight / 2) + $this->current;
return [
...range(start: 1, end: $omitLeftFrom - 1),
...[$this->indicator],
...range(start: $this->current - ($visibleLeft - $omitLeftFrom), end: $omitRightFrom - 1),
...[$this->indicator],
...range(start: $this->total - ($visibleRight - ($omitRightFrom - $this->current)) + 1, end: $this->total),
];
}
private function getPaginationData(): array
{
$this->validate();
if ($this->total <= $this->visible) {
return range(start: 1, end: $this->total);
}
if ($this->hasSingleOmitted()) {
return $this->getDataWithSingleOmitted();
}
return $this->getDataWithTwoOmitted();
}
private function getSingleBreakpoint(): int
{
return (int) floor(num: $this->visible / 2) + 1;
}
private function hasSingleOmitted(): bool
{
return $this->hasSingleOmittedNearLast() || $this->hasSingleOmittedNearStart();
}
private function hasSingleOmittedNearLast(): bool
{
return $this->current <= $this->getSingleBreakpoint();
}
private function hasSingleOmittedNearStart(): bool
{
return $this->current >= $this->total - $this->getSingleBreakpoint() + 1;
}
private function validate(): void
{
if ($this->total < 1) {
throw new InvalidArgumentException(message: sprintf('Total number of pages (%d) should not be lower than 1', $this->total));
}
if ($this->current < 1) {
throw new InvalidArgumentException(message: sprintf('Current page (%d) should not be lower than 1', $this->current));
}
if ($this->current > $this->total) {
throw new InvalidArgumentException(message: sprintf('Current page (%d) should not be higher than total number of pages (%d)', $this->current, $this->total));
}
if ($this->indicator >= 1 && $this->indicator <= $this->total) {
throw new InvalidArgumentException(message: sprintf('Omitted pages indicator (%d) should not be between 1 and total number of pages (%d)', $this->indicator, $this->total));
}
}
}