to composer
This commit is contained in:
68
src/Pagination.php
Normal file
68
src/Pagination.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
|
||||
class Pagination
|
||||
{
|
||||
|
||||
private string $html = "";
|
||||
private int $perPage = 10;
|
||||
private int $countItem;
|
||||
private int $currentPage = 1;
|
||||
private int $countPages;
|
||||
|
||||
private string $baseUrl;
|
||||
|
||||
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
|
||||
{
|
||||
$this->countItem = $countItem;
|
||||
$this->perPage = $perPage;
|
||||
$this->currentPage = $currentPage;
|
||||
$this->baseUrl = $baseUrl;
|
||||
|
||||
$this->countPages = ceil($this->countItem / $perPage);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
|
||||
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
||||
$btns = $prev ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$prev'>$prev</a></li>" : "";
|
||||
$btns .= "<li class='page-item'><a class='page-link active' href='#'>$this->currentPage</a></li>";
|
||||
$btns .= $next ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$next'>$next</a></li>" : "";
|
||||
|
||||
$this->html = str_replace('{btns}', $btns, $this->getTemplate());
|
||||
$this->html = str_replace('{previous_link}', $this->baseUrl . "/1", $this->html);
|
||||
$this->html = str_replace('{next_link}', $this->baseUrl . "/" . $this->countPages, $this->html);
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
echo $this->html;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
private function getTemplate()
|
||||
{
|
||||
return '<nav aria-label="Page navigation example">
|
||||
<ul class="pagination">
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{previous_link}" aria-label="Previous">
|
||||
<span aria-hidden="true">«</span>
|
||||
</a>
|
||||
</li>
|
||||
{btns}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{next_link}" aria-label="Next">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user