Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
196 lines (155 loc) · 4.85 KB

File metadata and controls

196 lines (155 loc) · 4.85 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Phenix\Database;
use League\Uri\Components\Query;
use League\Uri\Http;
use Phenix\Contracts\Arrayable;
use Phenix\Data\Collection;
use Phenix\Util\URL;
class Paginator implements Arrayable
{
private Query $query;
private int $itemsEachSide = 2;
private int $linksNumber = 5;
private readonly int $lastPage;
private bool $withQueryParameters = true;
public function __construct(
private Http $uri,
private Collection $data,
private readonly int $total,
private readonly int $currentPage,
private readonly int $perPage
) {
$this->query = Query::fromUri($this->uri);
$this->lastPage = (int) ceil($this->total / $this->perPage);
}
public function withoutQueryParameters(): self
{
$this->withQueryParameters = false;
return $this;
}
public function data(): Collection
{
return $this->data;
}
public function total(): int
{
return $this->total;
}
public function perPage(): int
{
return $this->perPage;
}
public function currentPage(): int
{
return $this->currentPage;
}
public function lastPage(): int
{
return $this->lastPage;
}
public function hasPreviousPage()
{
return $this->currentPage > 1;
}
public function hasNextPage()
{
return $this->currentPage < $this->lastPage;
}
public function from(): int
{
return (($this->currentPage - 1) * $this->perPage) + 1;
}
public function to(): int
{
if ($this->hasNextPage()) {
return $this->currentPage * $this->perPage;
}
return $this->total;
}
public function links(): array
{
$links = [];
$separator = ['url' => null, 'label' => '...'];
$prepend = ($this->currentPage + 1) - $this->itemsEachSide;
$prepend = $prepend < 0 ? 0 : $prepend;
if ($prepend > ($this->itemsEachSide + 1)) {
$prepend = $this->itemsEachSide;
$links[] = $this->buildLink(1);
$links[] = $separator;
}
$start = $this->currentPage - $prepend;
for ($i = $start; $i < $this->currentPage; $i++) {
$links[] = $this->buildLink($i);
}
$append = $this->linksNumber - $prepend;
$append = ($this->currentPage + $append) > $this->lastPage
? ($this->lastPage - $this->currentPage) + 1
: $append;
$limit = $this->currentPage + $append;
for ($i = $this->currentPage; $i < $limit; $i++) {
$links[] = $this->buildLink($i);
}
if (($this->lastPage - ($this->currentPage + $append)) >= 1) {
$links[] = $separator;
$links[] = $this->buildLink($this->lastPage);
}
if (($this->lastPage - ($this->currentPage + $append)) === 0) {
$links[] = $this->buildLink($this->lastPage);
}
return $links;
}
public function toArray(): array
{
return [
'path' => URL::build($this->uri->getPath()),
'current_page' => $this->currentPage,
'last_page' => $this->lastPage,
'per_page' => $this->perPage,
'total' => $this->total,
'first_page_url' => $this->getFirstPageUrl(),
'last_page_url' => $this->getLastPageUrl(),
'prev_page_url' => $this->getPrevPageUrl(),
'next_page_url' => $this->getNextPageUrl(),
'from' => $this->from(),
'to' => $this->to(),
'data' => $this->data->toArray(),
'links' => $this->links(),
];
}
private function getQueryParameters(): array
{
return $this->withQueryParameters
? $this->query->parameters()
: [];
}
private function buildLink(int $page, string|int|null $label = null): array
{
return ['url' => $this->buildPageUrl($page), 'label' => $label ?? $page];
}
private function buildPageUrl(int $page): string
{
$parameters['page'] = $page;
if ($this->query->has('per_page')) {
$parameters['per_page'] = $this->perPage;
}
$parameters = array_merge($this->getQueryParameters(), $parameters);
return URL::build($this->uri->getPath(), $parameters);
}
private function getFirstPageUrl(): string
{
return $this->buildPageUrl(1);
}
private function getLastPageUrl(): string
{
return $this->buildPageUrl($this->lastPage);
}
private function getPrevPageUrl(): string|null
{
return $this->hasPreviousPage() ? $this->buildPageUrl($this->currentPage - 1) : null;
}
private function getNextPageUrl(): string|null
{
return $this->hasNextPage() ? $this->buildPageUrl($this->currentPage + 1) : null;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.