Skip to content

Commit b77564c

Browse files
committed
Add getters and setters in Pager
1 parent 2451ca6 commit b77564c

File tree

1 file changed

+111
-11
lines changed

1 file changed

+111
-11
lines changed

src/Pager.php

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Pager implements JsonSerializable
3131
protected ?int $nextPage = null;
3232
protected int $lastPage;
3333
protected int $itemsPerPage;
34+
protected int $totalItems;
3435
protected int $surround = 2;
3536
/**
3637
* @var array<string,string>
@@ -109,19 +110,20 @@ public function __construct(
109110
if ($language) {
110111
$this->setLanguage($language);
111112
}
112-
$this->currentPage = static::sanitize($currentPage);
113-
$this->itemsPerPage = static::sanitize($itemsPerPage);
114-
$this->lastPage = (int) \ceil($totalItems / $this->itemsPerPage);
115-
$this->lastPage = static::sanitize($this->lastPage);
116-
if ($this->currentPage > 1) {
117-
if ($this->currentPage - 1 <= $this->lastPage) {
118-
$this->previousPage = $this->currentPage - 1;
119-
} elseif ($this->lastPage > 1) {
120-
$this->previousPage = $this->lastPage;
113+
$this->setCurrentPage(static::sanitize($currentPage))
114+
->setItemsPerPage(static::sanitize($itemsPerPage))
115+
->setTotalItems($totalItems);
116+
$lastPage = (int) \ceil($this->getTotalItems() / $this->getItemsPerPage());
117+
$this->setLastPage(static::sanitize($lastPage));
118+
if ($this->getCurrentPage() > 1) {
119+
if ($this->getCurrentPage() - 1 <= $this->getLastPage()) {
120+
$this->setPreviousPage($this->getCurrentPage() - 1);
121+
} elseif ($this->getLastPage() > 1) {
122+
$this->setPreviousPage($this->getLastPage());
121123
}
122124
}
123-
if ($this->currentPage < $this->lastPage) {
124-
$this->nextPage = $this->currentPage + 1;
125+
if ($this->getCurrentPage() < $this->getLastPage()) {
126+
$this->setNextPage($this->getCurrentPage() + 1);
125127
}
126128
isset($url) ? $this->setUrl($url) : $this->prepareUrl();
127129
}
@@ -131,6 +133,52 @@ public function __toString() : string
131133
return $this->render();
132134
}
133135

136+
/**
137+
* @since 3.5
138+
*
139+
* @return int
140+
*/
141+
public function getItemsPerPage() : int
142+
{
143+
return $this->itemsPerPage;
144+
}
145+
146+
/**
147+
* @since 3.5
148+
*
149+
* @param int $itemsPerPage
150+
*
151+
* @return static
152+
*/
153+
protected function setItemsPerPage(int $itemsPerPage) : static
154+
{
155+
$this->itemsPerPage = $itemsPerPage;
156+
return $this;
157+
}
158+
159+
/**
160+
* @since 3.5
161+
*
162+
* @return int
163+
*/
164+
public function getTotalItems() : int
165+
{
166+
return $this->totalItems;
167+
}
168+
169+
/**
170+
* @since 3.5
171+
*
172+
* @param int $totalItems
173+
*
174+
* @return static
175+
*/
176+
protected function setTotalItems(int $totalItems) : static
177+
{
178+
$this->totalItems = $totalItems;
179+
return $this;
180+
}
181+
134182
/**
135183
* @param Language|null $language
136184
*
@@ -286,6 +334,19 @@ public function getCurrentPage() : int
286334
return $this->currentPage;
287335
}
288336

337+
/**
338+
* @since 3.5
339+
*
340+
* @param int $currentPage
341+
*
342+
* @return static
343+
*/
344+
protected function setCurrentPage(int $currentPage) : static
345+
{
346+
$this->currentPage = $currentPage;
347+
return $this;
348+
}
349+
289350
public function getCurrentPageUrl() : string
290351
{
291352
return $this->getPageUrl($this->currentPage);
@@ -308,6 +369,19 @@ public function getLastPage() : int
308369
return $this->lastPage;
309370
}
310371

372+
/**
373+
* @since 3.5
374+
*
375+
* @param int $lastPage
376+
*
377+
* @return static
378+
*/
379+
protected function setLastPage(int $lastPage) : static
380+
{
381+
$this->lastPage = $lastPage;
382+
return $this;
383+
}
384+
311385
public function getLastPageUrl() : string
312386
{
313387
return $this->getPageUrl($this->getLastPage());
@@ -319,6 +393,19 @@ public function getPreviousPage() : ?int
319393
return $this->previousPage;
320394
}
321395

396+
/**
397+
* @since 3.5
398+
*
399+
* @param int $previousPage
400+
*
401+
* @return static
402+
*/
403+
protected function setPreviousPage(int $previousPage) : static
404+
{
405+
$this->previousPage = $previousPage;
406+
return $this;
407+
}
408+
322409
public function getPreviousPageUrl() : ?string
323410
{
324411
return $this->getPageUrl($this->getPreviousPage());
@@ -330,6 +417,19 @@ public function getNextPage() : ?int
330417
return $this->nextPage;
331418
}
332419

420+
/**
421+
* @since 3.5
422+
*
423+
* @param int $nextPage
424+
*
425+
* @return static
426+
*/
427+
protected function setNextPage(int $nextPage) : static
428+
{
429+
$this->nextPage = $nextPage;
430+
return $this;
431+
}
432+
333433
public function getNextPageUrl() : ?string
334434
{
335435
return $this->getPageUrl($this->getNextPage());

0 commit comments

Comments
 (0)