1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\View\Helper; |
4
|
|
|
|
5
|
|
|
use Bone\View\Helper\Exception\PaginatorException; |
6
|
|
|
use Del\Icon; |
7
|
|
|
|
8
|
|
|
class Paginator |
9
|
|
|
{ |
10
|
|
|
private $currentPage = 1; |
11
|
|
|
private $pageCount; |
12
|
|
|
private $pagerSize = 5; |
13
|
|
|
private $url; |
14
|
|
|
private $urlPart = ':page'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param int $pageNum |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
5 |
|
private function url(int $pageNum): string |
21
|
|
|
{ |
22
|
5 |
|
return str_replace($this->urlPart, $pageNum, $this->url); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $pageCount |
27
|
|
|
*/ |
28
|
2 |
|
public function setPageCount(int $pageCount): void |
29
|
|
|
{ |
30
|
2 |
|
$this->pageCount = $pageCount; |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $pageCount |
35
|
|
|
*/ |
36
|
5 |
|
public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void |
37
|
|
|
{ |
38
|
5 |
|
$this->pageCount = (int) ceil($rowCount / $numPerPage) ?: 1; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $url |
43
|
|
|
*/ |
44
|
6 |
|
public function setUrl(string $url): void |
45
|
|
|
{ |
46
|
6 |
|
$this->url = $url; |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $replace |
51
|
|
|
*/ |
52
|
1 |
|
public function setUrlPart(string $replace): void |
53
|
|
|
{ |
54
|
1 |
|
$this->urlPart = $replace; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param int $page_no |
59
|
|
|
*/ |
60
|
6 |
|
public function setCurrentPage(int $page_no): void |
61
|
|
|
{ |
62
|
6 |
|
$this->currentPage = $page_no; |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $numBoxes an ODD number! |
67
|
|
|
*/ |
68
|
7 |
|
public function setPagerSize(int $numBoxes): void |
69
|
|
|
{ |
70
|
7 |
|
if ($numBoxes % 2 === 0) { |
71
|
3 |
|
$numBoxes--; |
72
|
|
|
} |
73
|
7 |
|
$this->pagerSize = $numBoxes; |
74
|
7 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
7 |
|
public function getPagerSize(): int |
80
|
|
|
{ |
81
|
7 |
|
return $this->pagerSize; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws PaginatorException |
86
|
|
|
*/ |
87
|
8 |
|
private function ensurePageCount() |
88
|
|
|
{ |
89
|
8 |
|
if (null === $this->pageCount) { |
90
|
1 |
|
throw new PaginatorException(PaginatorException::NO_PAGE_COUNT); |
91
|
|
|
} |
92
|
7 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws PaginatorException |
96
|
|
|
*/ |
97
|
7 |
|
private function ensureUrl() |
98
|
|
|
{ |
99
|
7 |
|
if (null === $this->url) { |
100
|
1 |
|
throw new PaginatorException(PaginatorException::NO_URL); |
101
|
|
|
} |
102
|
6 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
5 |
|
private function calculateStart(int $pages): int |
108
|
|
|
{ |
109
|
5 |
|
$half = ($pages - 1) / 2; |
110
|
|
|
|
111
|
5 |
|
if ($this->currentPage < 3) { |
112
|
2 |
|
$start = 1; |
113
|
3 |
|
} elseif ($this->currentPage >= ($this->pageCount - $half)) { |
114
|
1 |
|
$start = $this->pageCount - ($this->getPagerSize() - 1); |
115
|
|
|
} else { |
116
|
2 |
|
$start = $this->currentPage - $half; |
117
|
|
|
|
118
|
2 |
|
if ($start < 1) { |
119
|
1 |
|
$start = 1; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
return $start; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
* @throws PaginatorException |
129
|
|
|
*/ |
130
|
8 |
|
public function render(): string |
131
|
|
|
{ |
132
|
8 |
|
$this->ensurePageCount(); |
133
|
7 |
|
$this->ensureUrl(); |
134
|
|
|
|
135
|
6 |
|
$html = '<nav><ul class="pagination">'; |
136
|
|
|
|
137
|
6 |
|
if ($this->pageCount > ($this->getPagerSize() - 1)) { |
138
|
5 |
|
$pages = $this->getPagerSize(); |
139
|
5 |
|
$start = $this->calculateStart($pages); |
140
|
|
|
} else { |
141
|
1 |
|
$pages = $this->pageCount; |
142
|
1 |
|
$start = 1; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
$html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">'; |
146
|
6 |
|
if ($this->currentPage === 1) { |
147
|
2 |
|
$html .= '<a class="page-link" href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>'; |
148
|
|
|
} else { |
149
|
4 |
|
$html .= '<a class="page-link" href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>'; |
150
|
|
|
} |
151
|
6 |
|
$html .= '</li>'; |
152
|
|
|
|
153
|
6 |
|
$html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">'; |
154
|
6 |
View Code Duplication |
if ($this->currentPage === 1) { |
|
|
|
|
155
|
2 |
|
$html .= '<a class="page-link" href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>'; |
156
|
|
|
} else { |
157
|
4 |
|
$html .= '<a class="page-link" href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>'; |
158
|
|
|
} |
159
|
6 |
|
$html .= '</li>'; |
160
|
|
|
|
161
|
6 |
|
for ($x = $start; $x <= ($start + ($pages - 1)); $x++) { |
162
|
6 |
|
$html .= '<li class="page-item '; |
163
|
6 |
|
if ($this->currentPage === $x) { |
164
|
6 |
|
$html .= ' active" aria-current="page'; |
165
|
|
|
} |
166
|
6 |
|
$html .= '">'; |
167
|
6 |
|
if ($this->currentPage === $x) { |
168
|
6 |
|
$html .= '<a class="page-link" href="#">' . $x . '</a>'; |
169
|
|
|
} else { |
170
|
5 |
|
$html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>'; |
171
|
|
|
} |
172
|
6 |
|
$html .= '</li>'; |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
$html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">'; |
176
|
6 |
View Code Duplication |
if ($this->currentPage >= $this->pageCount) { |
|
|
|
|
177
|
2 |
|
$html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>'; |
178
|
|
|
} else { |
179
|
4 |
|
$html .= '<a class="page-link" href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>'; |
180
|
|
|
} |
181
|
6 |
|
$html .= '</li>'; |
182
|
|
|
|
183
|
6 |
|
$html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">'; |
184
|
6 |
View Code Duplication |
if ($this->currentPage >= $this->pageCount) { |
|
|
|
|
185
|
2 |
|
$html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>'; |
186
|
|
|
} else { |
187
|
4 |
|
$html .= '<a class="page-link" href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>'; |
188
|
|
|
} |
189
|
6 |
|
$html .= '</li>'; |
190
|
6 |
|
$html .= '</ul></nav>'; |
191
|
|
|
|
192
|
6 |
|
return $html; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.