1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace KadokWeb\Paginator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class KadokWeb Paginator |
8
|
|
|
* |
9
|
|
|
* @author Doka B. Silva <https://github.com/kadokweb> |
10
|
|
|
* @package KadokWeb\Paginator |
11
|
|
|
*/ |
12
|
|
|
class Paginator |
13
|
|
|
{ |
14
|
|
|
/** @var int */ |
15
|
|
|
private $page; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $pages; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
private $rows; |
22
|
|
|
|
23
|
|
|
/** @var int */ |
24
|
|
|
private $limit; |
25
|
|
|
|
26
|
|
|
/** @var int */ |
27
|
|
|
private $offset; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $range; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $link; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $title; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $class; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
private $hash; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
private $first; |
46
|
|
|
|
47
|
|
|
/** @var array */ |
48
|
|
|
private $last; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Paginator constructor. |
52
|
|
|
* @param string|null $link |
53
|
|
|
* @param string|null $title |
54
|
|
|
* @param array|null $first |
55
|
|
|
* @param array|null $last |
56
|
|
|
*/ |
57
|
|
|
public function __construct(string $link = null, string $title = null, array $first = null, array $last = null) |
58
|
|
|
{ |
59
|
|
|
$this->link = ($link ?? "?page="); |
60
|
|
|
$this->title = ($title ?? "Página"); |
61
|
|
|
$this->first = ($first ?? ["Primeira página", "<<"]); |
62
|
|
|
$this->last = ($last ?? ["Última página", ">>"]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $rows |
67
|
|
|
* @param int $limit |
68
|
|
|
* @param int|null $page |
69
|
|
|
* @param int $range |
70
|
|
|
* @param string|null $hash |
71
|
|
|
*/ |
72
|
|
|
public function pager(int $rows, int $limit = 10, int $page = null, int $range = 3, string $hash = null): void |
73
|
|
|
{ |
74
|
|
|
$this->rows = $this->toPositive($rows); |
75
|
|
|
$this->limit = $this->toPositive($limit); |
76
|
|
|
$this->range = $this->toPositive($range); |
77
|
|
|
$this->pages = (int)ceil($this->rows / $this->limit); |
78
|
|
|
$this->page = ($page <= $this->pages ? $this->toPositive($page) : $this->pages); |
79
|
|
|
|
80
|
|
|
$this->offset = (($this->page * $this->limit) - $this->limit >= 0 ? ($this->page * $this->limit) - $this->limit : 0); |
81
|
|
|
$this->hash = (!empty($hash) ? "#{$hash}" : null); |
82
|
|
|
|
83
|
|
|
if ($this->rows && $this->offset >= $this->rows) { |
84
|
|
|
header("Location: {$this->link}" . ceil($this->rows / $this->limit)); |
85
|
|
|
exit; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function limit(): int |
93
|
|
|
{ |
94
|
|
|
return $this->limit; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function offset(): int |
101
|
|
|
{ |
102
|
|
|
return $this->offset; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function page() |
109
|
|
|
{ |
110
|
|
|
return $this->page; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
public function pages() |
117
|
|
|
{ |
118
|
|
|
return $this->pages; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $cssClass |
123
|
|
|
* @return null|string |
124
|
|
|
*/ |
125
|
|
|
public function render(string $cssClass = "paginator"): ?string |
126
|
|
|
{ |
127
|
|
|
$this->class = $cssClass; |
128
|
|
|
|
129
|
|
|
if ($this->rows > $this->limit): |
130
|
|
|
$paginator = "<nav class=\"{$this->class}\">"; |
131
|
|
|
$paginator .= "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}\">{$this->first[1]}</a>"; |
132
|
|
|
$paginator .= $this->beforePages(); |
133
|
|
|
$paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>"; |
134
|
|
|
$paginator .= $this->afterPages(); |
135
|
|
|
$paginator .= "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}\">{$this->last[1]}</a>"; |
136
|
|
|
$paginator .= "</nav>"; |
137
|
|
|
return $paginator; |
138
|
|
|
endif; |
139
|
|
|
|
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return null|string |
145
|
|
|
*/ |
146
|
|
|
private function beforePages(): ?string |
147
|
|
|
{ |
148
|
|
|
$before = null; |
149
|
|
|
for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++): |
150
|
|
|
if ($iPag >= 1): |
151
|
|
|
$before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}\">{$iPag}</a>"; |
152
|
|
|
endif; |
153
|
|
|
endfor; |
154
|
|
|
|
155
|
|
|
return $before; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string|null |
160
|
|
|
*/ |
161
|
|
|
private function afterPages(): ?string |
162
|
|
|
{ |
163
|
|
|
$after = null; |
164
|
|
|
for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++): |
165
|
|
|
if ($dPag <= $this->pages): |
166
|
|
|
$after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}\">{$dPag}</a>"; |
167
|
|
|
endif; |
168
|
|
|
endfor; |
169
|
|
|
|
170
|
|
|
return $after; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $number |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
private function toPositive($number): int |
178
|
|
|
{ |
179
|
|
|
return ($number >= 1 ? $number : 1); |
180
|
|
|
} |
181
|
|
|
} |