-
-
Save Stalker-Red/2dcf2e91db7428ee5274b9271a28a17f to your computer and use it in GitHub Desktop.
php paginator markup generator
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 | |
namespace utils\markup\paginator; | |
class Paginator{ | |
private $template; | |
private $templateDefault = [ | |
'prev' => '<li><a href="*url*">«</a></li>', | |
'prev_disabled' => '', | |
'next' => '<li><a href="*url*">»</a></li>', | |
'next_disabled' => '', | |
'link' => '<li><a href="*url*">*pageNumber*</a></li>', | |
'link_active' => '<li class="active"><a href="*url*" >*pageNumber*</a></li>', | |
'gap' => '<li class="disabled">…</li>', | |
'wrapper' => '<nav class="paginator"><ul class="paginator">*links*</ul></nav>' | |
]; | |
private $urlPrefix; | |
private $totalPages; | |
private $activePage; | |
private $nextURL; | |
private $prevURL; | |
private $gap1Start; | |
private $gap1End; | |
private $gap2Start; | |
private $gap2End; | |
private $gap1Length; | |
private $gap2Length; | |
private $HTML = ''; | |
/** | |
* Paginator constructor. | |
* @param $activePage | |
* @param $perPage | |
* @param $itemsCount | |
* @param string $urlPrefix | |
* @param int $indent | |
* @param array $template | |
* @return self | |
*/ | |
public function __construct($activePage, $perPage, $itemsCount, $urlPrefix = '', $indent = 0, $template = []) | |
{ | |
$this->urlPrefix = $urlPrefix ? $urlPrefix.'&page=' : '?page='; | |
$this->totalPages = ceil($itemsCount / $perPage); | |
$this->activePage = (int) $activePage; | |
$this->prevURL = $this->urlPrefix . ($activePage - 1); | |
$this->nextURL = $this->urlPrefix . ($activePage + 1); | |
if ($indent) { | |
// 1 2 3 ... 6 7 8 9 10 ... 18 19 20 | |
$this->gap1Start = $indent + 1; | |
$this->gap1End = $this->activePage - $indent; | |
$this->gap2Start = $this->activePage + $indent + 1; | |
$this->gap2End = $this->totalPages - $indent + 1; | |
$this->gap1Length = $this->gap1End - $this->gap1Start; | |
$this->gap2Length = $this->gap2End - $this->gap2Start; | |
if ($this->gap1Length < 2) $this->gap1End = $this->gap1Start; | |
if ($this->gap2Length < 2) $this->gap2End = $this->gap2Start; | |
} else { | |
// if no indent, all links generated in last cycle (see gap2End) | |
$this->gap1Length = $gap2Length = $gap1Start = $gap1End = $gap2Start = 0; | |
$this->gap2End = 1; | |
} | |
$this->setTemplate($template); | |
return $this; | |
} | |
/** | |
* Объединяет templateDefault c переданным | |
* @param array $template with html parts. Look for example at $this->templateDefault | |
* @return self | |
*/ | |
public function setTemplate($template) | |
{ | |
$this->template = array_merge($this->templateDefault, $template); | |
return $this; | |
} | |
public function __toString() | |
{ | |
$this->generate(); | |
return $this->HTML; | |
} | |
/** | |
* send HTML to output | |
* @return $this | |
*/ | |
public function renderHTML() | |
{ | |
$this->generate(); | |
echo $this->HTML; | |
return $this; | |
} | |
private function generate() { | |
$links = ''; | |
if ($this->activePage > 1) { // previous | |
$links .= str_replace('*url*', $this->prevURL, $this->template['prev']); | |
} else { | |
$links .= str_replace('*url*', $this->prevURL, $this->template['prev_disabled']); | |
} | |
for ($p = 1; $p < $this->gap1Start; $p++) | |
$links .= $this->generateLink($p); | |
if ($this->gap1Length > 1) $links .= $this->generateGap(); | |
for ($p = $this->gap1End; $p < $this->gap2Start && $p <= $this->totalPages; $p++) | |
$links .= $this->generateLink($p); | |
if ($this->gap2Length > 1) $links .= $this->generateGap(); | |
for ($p = $this->gap2End; $p <= $this->totalPages; $p++) | |
$links .= $this->generateLink($p); | |
if ($this->activePage < $this->totalPages) { // next | |
$links .= str_replace('*url*', $this->nextURL, $this->template['next']); | |
} else { | |
$links .= str_replace('*url*', $this->nextURL, $this->template['next_disabled']); | |
} | |
$this->HTML = str_replace('*links*', PHP_EOL . $links . PHP_EOL, $this->template['wrapper']); | |
} | |
private function generateLink($page) | |
{ | |
if ($page === $this->activePage) { | |
return str_replace(['*url*', '*pageNumber*'], [$this->urlPrefix . $page, $page], $this->template['link_active']) . PHP_EOL; | |
} else { | |
return str_replace(['*url*', '*pageNumber*'], [$this->urlPrefix . $page, $page], $this->template['link']) . PHP_EOL; | |
} | |
} | |
private function generateGap() | |
{ | |
return $this->template['gap'] . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment