Skip to content

Commit 09c6d15

Browse files
committed
Added renderHtml() method and updated examples
1 parent b2dbe02 commit 09c6d15

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

examples/PDOExample.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,4 @@
5050

5151
echo '<hr/>';
5252

53-
foreach ($pages as $pageObj) {
54-
echo '<li>';
55-
echo '<a href="?page='.$pageObj->id.'">';
56-
if ($pageObj->label === 'current') {
57-
echo "<b>{$pageObj->id}</b>";
58-
} else {
59-
echo $pageObj->label;
60-
}
61-
echo '</a>';
62-
echo '</li>';
63-
}
53+
echo $pagination->renderHtml();

src/Pagination.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Pagination
4141
*/
4242
const LABEL_CURRENT = 'current';
4343

44+
/**
45+
* Label for numeric
46+
*/
47+
const LABEL_NUMERIC = 'numeric';
48+
4449
/**
4550
* Base integer of first page
4651
*/
@@ -74,6 +79,8 @@ class Pagination
7479
*/
7580
protected $numeric_links_depth = 3;
7681

82+
protected $line_format = [];
83+
7784
/**
7885
* Toggle numeric links
7986
*
@@ -104,7 +111,10 @@ class Pagination
104111
*/
105112
public function __construct($total_items, $current_page, $items_per_page = 10)
106113
{
107-
$this->totalItems($total_items)->currentPage($current_page)->itemsPerPage($items_per_page);
114+
$this->totalItems($total_items)
115+
->currentPage($current_page)
116+
->itemsPerPage($items_per_page)
117+
->setLineFormat('<li class="@class@"><a href="?page=@id@">@label@</a></li>');
108118
}
109119

110120
/**
@@ -205,7 +215,7 @@ public function parse()
205215
$pagination_data = [];
206216

207217
if ($this->total_items === 0) {
208-
return (object)$pagination_data;
218+
return $pagination_data;
209219
}
210220

211221
$total_pages = ceil($this->total_items/$this->items_per_page);
@@ -214,39 +224,78 @@ public function parse()
214224

215225
// First
216226
if ($this->first_last_links && $this->current_page - $this->numeric_links_depth > self::BASE_PAGE) {
217-
$pagination_data[] = (object)['label' => self::LABEL_FIRST, 'id' => self::BASE_PAGE];
227+
$pagination_data[] = ['label' => self::LABEL_FIRST, 'id' => self::BASE_PAGE];
218228
}
219229

220230
// Previous
221231
if ($this->current_page > self::BASE_PAGE) {
222-
$pagination_data[] = (object)['label' => self::LABEL_PREV, 'id' => $this->current_page - 1];
232+
$pagination_data[] = ['label' => self::LABEL_PREV, 'id' => $this->current_page - 1];
223233
}
224234

225235
// Numeric
226236
if ($this->numeric_links) {
227237
for ($i = max(self::BASE_PAGE, $this->current_page - $this->numeric_links_depth);
228238
$i <= min($this->current_page + $this->numeric_links_depth, $total_pages); $i++) {
229239
if ($i === $this->current_page) {
230-
$pagination_data[] = (object)['label' => self::LABEL_CURRENT, 'id' => $i];
240+
$pagination_data[] = ['label' => self::LABEL_CURRENT, 'id' => $i];
231241
continue;
232242
}
233243

234-
$pagination_data[] = (object)['label' => $i, 'id' => $i];
244+
$pagination_data[] = ['label' => self::LABEL_NUMERIC, 'id' => $i];
235245
}
236246
}
237247
// Next
238248
if ($this->current_page < $total_pages) {
239-
$pagination_data[] = (object)['label' => self::LABEL_NEXT, 'id' => $this->current_page + 1];
249+
$pagination_data[] = ['label' => self::LABEL_NEXT, 'id' => $this->current_page + 1];
240250
}
241251

242252
// Last
243253
if ($this->first_last_links && $this->current_page < $total_pages - $this->numeric_links_depth) {
244-
$pagination_data[] = (object)['label' => self::LABEL_LAST, 'id' => $total_pages];
254+
$pagination_data[] = ['label' => self::LABEL_LAST, 'id' => $total_pages];
245255
}
246256

247257
return (object)$pagination_data;
248258
}
249259

260+
public function renderHtml($before = '<ul class="pagination">', $after = '</ul>')
261+
{
262+
$pages = $this->parse();
263+
$html = '';
264+
if (empty($pages)) {
265+
return $html;
266+
}
267+
268+
$html .= $before;
269+
$class = $this->line_format['idle_class'];
270+
271+
foreach ($pages as $page) {
272+
$label = $page['id'];
273+
274+
if ($page['label'] === self::LABEL_CURRENT) {
275+
$class = $this->line_format['active_class'];
276+
} elseif ($page['label'] === self::LABEL_FIRST) {
277+
$label = 'First';
278+
} elseif ($page['label'] === self::LABEL_LAST) {
279+
$label = 'Last';
280+
} elseif ($page['label'] === self::LABEL_NEXT) {
281+
$label = 'Next';
282+
} elseif ($page['label'] === self::LABEL_PREV) {
283+
$label = 'Prev.';
284+
}
285+
$parser = ['@id@' => $page['id'], '@class@' => $class, '@label@' => $label];
286+
$html .= str_ireplace(array_keys($parser), array_values($parser), $this->line_format['format']);
287+
}
288+
289+
$html .= $after;
290+
return $html;
291+
}
292+
293+
public function setLineFormat($format, $active_class = 'active', $idle_class = 'page_link')
294+
{
295+
$this->line_format = ['format' => $format, 'active_class' => $active_class, 'idle_class' => $idle_class];
296+
return $this;
297+
}
298+
250299
/**
251300
* Returns the offset value, self::parse() must be called before calling this
252301
*

0 commit comments

Comments
 (0)