diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ee5a02..c7edec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.5.2 + +* refactor: `PagerItemInterface` methods now returns itself, instead of + `PageInterface` + ## 0.5.0 * build: initial commit diff --git a/packages/rekapager-core/src/Contracts/PagerItemInterface.php b/packages/rekapager-core/src/Contracts/PagerItemInterface.php index d4312d5..0831cfe 100644 --- a/packages/rekapager-core/src/Contracts/PagerItemInterface.php +++ b/packages/rekapager-core/src/Contracts/PagerItemInterface.php @@ -25,10 +25,35 @@ interface PagerItemInterface extends PageInterface { public function getUrl(): ?string; + public function isDisabled(): bool; + + // + // overriden methods + // + /** - * @return PageInterface + * @return null|PagerItemInterface */ - public function getPage(): PageInterface; + public function getNextPage(): ?PagerItemInterface; - public function isDisabled(): bool; + /** + * @return null|PagerItemInterface + */ + public function getPreviousPage(): ?PagerItemInterface; + + /** + * Gets n next pages + * + * @param int<1,max> $numberOfPages + * @return array> + */ + public function getNextPages(int $numberOfPages): array; + + /** + * Gets n previous pages + * + * @param int<1,max> $numberOfPages + * @return array> + */ + public function getPreviousPages(int $numberOfPages): array; } diff --git a/packages/rekapager-core/src/Pager/Internal/PagerItem.php b/packages/rekapager-core/src/Pager/Internal/PagerItem.php index 34efe6c..cfffd1f 100644 --- a/packages/rekapager-core/src/Pager/Internal/PagerItem.php +++ b/packages/rekapager-core/src/Pager/Internal/PagerItem.php @@ -44,11 +44,6 @@ public function __construct( $this->pageNumber = new NullPageNumber(); } - public function getPage(): PageInterface - { - return $this->wrapped; - } - public function isDisabled(): bool { return $this->wrapped instanceof NullPageInterface; @@ -88,24 +83,48 @@ public function getItemsPerPage(): int return $this->wrapped->getItemsPerPage(); } - public function getNextPage(): ?PageInterface + public function getNextPage(): ?PagerItemInterface { - return $this->wrapped->getNextPage(); + $nextPage = $this->wrapped->getNextPage(); + + if ($nextPage === null) { + return null; + } + + return $this->decorate($nextPage); } - public function getPreviousPage(): ?PageInterface + public function getPreviousPage(): ?PagerItemInterface { - return $this->wrapped->getPreviousPage(); + $previousPage = $this->wrapped->getPreviousPage(); + + if ($previousPage === null) { + return null; + } + + return $this->decorate($previousPage); } public function getNextPages(int $numberOfPages): array - { - return $this->wrapped->getNextPages($numberOfPages); + { + $pages = []; + + foreach ($this->wrapped->getNextPages($numberOfPages) as $page) { + $pages[] = $this->decorate($page); + } + + return $pages; } public function getPreviousPages(int $numberOfPages): array { - return $this->wrapped->getPreviousPages($numberOfPages); + $pages = []; + + foreach ($this->wrapped->getPreviousPages($numberOfPages) as $page) { + $pages[] = $this->decorate($page); + } + + return $pages; } public function count(): int @@ -117,6 +136,25 @@ public function getUrl(): ?string { return $this->pagerUrlGenerator->generateUrl($this); } + + /** + * @template TKey2 of array-key + * @template T2 + * @template TIdentifier2 of object + * @param PageInterface $page + * @return PagerItem + */ + private function decorate(PageInterface $page): PagerItem + { + if ($page instanceof PagerItem) { + return $page; + } + + return new PagerItem( + wrapped: $page, + pagerUrlGenerator: $this->pagerUrlGenerator, + ); + } } /**