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