From f5f6504545155e7a4efe11f3eea93ac61ef108bb Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 31 Jul 2024 15:49:40 +0300 Subject: [PATCH] pagination fix --- examples/simple.json | 1 + src/ListJsonTable.php | 21 +++++++++++++++------ src/Pagination.php | 23 +++++++++++++++++------ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/examples/simple.json b/examples/simple.json index 36a528a..92e9d01 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -10,6 +10,7 @@ "perPage": "5", "currentPage": "1", "total": 10, + "prefix": "/page", "params": {"class": "table table-bordered", "border": "1"} }, "data": [ diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index ca6ae7b..0bafe18 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -2,6 +2,7 @@ namespace Itguild\Tables; +use Exception; use Itguild\Tables\ActionColumn\ActionColumn; use Itguild\Tables\ActionColumn\DeleteActionColumn; use Itguild\Tables\ActionColumn\EditActionColumn; @@ -170,6 +171,9 @@ class ListJsonTable return $actions; } + /** + * @throws Exception + */ public function create(): void { $this->setActions(); @@ -192,6 +196,9 @@ class ListJsonTable } } + /** + * @throws Exception + */ public function endTable(): void { $this->html .= ""; @@ -199,12 +206,14 @@ class ListJsonTable $this->html .= $hookAfter($this->data['meta']); if ($this->pagination){ - $pagination = new Pagination( - $this->data['meta']['total'], - $this->data['meta']['perPage'], - $this->data['meta']['currentPage'], - $this->baseUrl - ); + $options = [ + 'countItem' => $this->data['meta']['total'], + 'perPage' => $this->data['meta']['perPage'], + 'currentPage' => $this->data['meta']['currentPage'], + 'baseUrl' => $this->baseUrl, + 'prefix' => $this->data['meta']['prefix'], + ]; + $pagination = new Pagination($options); $pagination->create(); $this->html .= $pagination->fetch(); } diff --git a/src/Pagination.php b/src/Pagination.php index 3f3b1f5..c5149a5 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -3,6 +3,9 @@ namespace Itguild\Tables; +use Exception; +use http\Message; + class Pagination { @@ -14,13 +17,21 @@ class Pagination private string $baseUrl; - public function __construct($countItem, $perPage, $currentPage, $baseUrl) -// public function __construct(int $countItem, array $params, string $baseUrl) +// public function __construct($countItem, $perPage, $currentPage, $baseUrl) + + /** + * @throws Exception + */ + public function __construct(array $options,) { - $this->countItem = $countItem; - $this->perPage = $perPage; - $this->currentPage = $currentPage; - $this->baseUrl = $baseUrl; + if (!$options['countItem']) { + throw new Exception(message: "countItem is not valid"); + } + $this->countItem = $options['countItem']; + $this->perPage = $options['perPage'] ?? 10; + $this->currentPage = $options['currentPage'] ?? 1; + $this->baseUrl = $options['baseUrl']; + $this->baseUrl .= $options['prefix'] ?? '/page'; $this->countPages = ceil($this->countItem / $this->perPage); }