Last active
July 9, 2019 15:27
-
-
Save delboy1978uk/681040ceb09333e438ed003b866d9a96 to your computer and use it in GitHub Desktop.
paginator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PaginatorException extends Exception | |
{ | |
public const NO_PAGE_COUNT = 'No total page count'; | |
public const NO_URL = 'No URL set'; | |
public const NO_URL_PART = 'No URL part set'; | |
public const NO_CURRENT_PAGE = 'No current page count'; | |
} | |
class Paginator | |
{ | |
private $currentPage = 1; | |
private $customNext; | |
private $customPrev; | |
private $pageCount; | |
private $pagerSize = 5; | |
private $url; | |
private $urlPart; | |
/** | |
* @param int $pageNum | |
* @return string | |
*/ | |
private function url(int $pageNum): string | |
{ | |
return str_replace($this->urlPart, $pageNum, $this->url); | |
} | |
/** | |
* @param $pageCount | |
*/ | |
public function setPagecount($pageCount): void | |
{ | |
$this->pageCount = $pageCount; | |
} | |
/** | |
* @param $url | |
*/ | |
public function setUrl($url): void | |
{ | |
$this->url = $url; | |
} | |
/** | |
* @param $replace | |
*/ | |
public function setUrlPart($replace): void | |
{ | |
$this->urlPart = $replace; | |
} | |
/** | |
* @param int $page_no | |
*/ | |
public function setCurrentPage(int $page_no): void | |
{ | |
$this->currentPage = $page_no; | |
} | |
/** | |
* @param $url | |
*/ | |
public function setCustomPrev($url): void | |
{ | |
$this->customPrev = '<a href="' . $url . '/"><i class="icon-backward"></i></a>'; | |
} | |
/** | |
* @param $url | |
*/ | |
public function setCustomNext(string $url) | |
{ | |
$this->customNext = '<a href="' . $url . '/"><i class="icon-forward"></i></a>'; | |
} | |
/** | |
* @param int $numBoxes an ODD number! | |
*/ | |
public function setPagerSize(int $numBoxes): void | |
{ | |
if ($numBoxes % 2 === 0) { | |
$numBoxes--; | |
} | |
$this->pagerSize = $numBoxes; | |
} | |
/** | |
* @return int | |
*/ | |
public function getPagerSize(): int | |
{ | |
if (!$this->pagerSize) { | |
$this->pagerSize = 5; | |
} | |
return $this->pagerSize; | |
} | |
/** | |
* @return string | |
* @throws PaginatorException | |
*/ | |
public function render(): string | |
{ | |
if (!$this->pageCount) { | |
throw new PaginatorException(PaginatorException::NO_PAGE_COUNT); | |
} | |
if (!$this->url) { | |
throw new PaginatorException(PaginatorException::NO_URL); | |
} | |
if (!$this->urlPart) { | |
throw new PaginatorException(PaginatorException::NO_URL_PART); | |
} | |
if (!$this->currentPage) { | |
throw new PaginatorException(PaginatorException::NO_CURRENT_PAGE); | |
} | |
$html = '<nav><ul class="pagination">'; | |
if ($this->pageCount > ($this->getPagerSize() - 1)) { | |
$pages = $this->getPagerSize(); | |
$half = ($pages - 1) / 2; | |
if ($this->currentPage === 1) { | |
$start = 1; | |
} elseif ($this->currentPage === 2) { | |
$start = 1; | |
} elseif ($this->currentPage >= ($this->pageCount - $half)) { | |
$start = $this->pageCount - ($this->getPagerSize() - 1); | |
} else { | |
$start = $this->currentPage - $half; | |
if ($start < 1) { | |
$start = 1; | |
} | |
} | |
} else { | |
$pages = $this->pageCount; | |
$start = 1; | |
} | |
$html .= ($start === 1) ? '<li class="page-item disabled">' :'<li class="page-item">'; | |
if (isset($this->customPrev)) { | |
$html .= $this->customPrev; | |
} elseif ($start === 1) { | |
$html .= '<a class="page-link" href ="#"><i class="icon-backward icon-disabled"></i></a>'; | |
} else { | |
$html .= '<a class="page-link" href ="' . $this->url(1) . '"><i class="icon-backward icon-active"></i></a>'; | |
} | |
$html .= '</li>'; | |
for ($x = $start; $x <= ($start + ($pages - 1)); $x++) { | |
$html .= '<li class="page-item '; | |
if ($this->currentPage === $x) { | |
$html .= ' active" aria-current="page'; | |
} | |
$html .= '">'; | |
if ($this->currentPage === $x) { | |
$html .= '<a class="page-link" href="#">' . $x . '</a>'; | |
} else { | |
$html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>'; | |
} | |
$html .= '</li>'; | |
} | |
$html .= (($start + ($pages - 1)) >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">'; | |
if (isset($this->customNext)) { | |
$html .= $this->customNext; | |
} elseif (($start + ($pages - 1)) >= $this->pageCount) { | |
$html .= '<span><i class="icon-forward icon-disabled"></i></span>'; | |
} else { | |
$html .= '<a class="page-link" href ="' . $this->url($this->pageCount) . '"><i class="icon-forward icon-active"></i></a>'; | |
} | |
$html .= '</li>'; | |
$html .= '</ul></nav>'; | |
return $html; | |
} | |
} | |
$page = $_GET['page'] ?: 1; | |
$pager = new Paginator(); | |
$pager->setCurrentPage($page); | |
$pager->setPagerSize(7); | |
$pager->setPagecount(15); | |
$pager->setUrl('http://dev.marshsat2.emea.mrshmc.com/dmclean2/cleva/be/sip/test.php?page=:page'); | |
$pager->setUrlPart(':page'); | |
?> | |
<html> | |
<head> | |
<link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | |
</head> | |
<body> | |
<?= $pager->render() ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment