Skip to content

Commit

Permalink
refactor: PagerItemInterface methods now returns itself, instead of…
Browse files Browse the repository at this point in the history
… `PageInterface` (#2)
  • Loading branch information
priyadi authored Apr 1, 2024
1 parent 481188a commit 517a858
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.5.2

* refactor: `PagerItemInterface` methods now returns itself, instead of
`PageInterface`

## 0.5.0

* build: initial commit
31 changes: 28 additions & 3 deletions packages/rekapager-core/src/Contracts/PagerItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,35 @@ interface PagerItemInterface extends PageInterface
{
public function getUrl(): ?string;

public function isDisabled(): bool;

//
// overriden methods
//

/**
* @return PageInterface<TKey,T,TIdentifier>
* @return null|PagerItemInterface<TKey,T,TIdentifier>
*/
public function getPage(): PageInterface;
public function getNextPage(): ?PagerItemInterface;

public function isDisabled(): bool;
/**
* @return null|PagerItemInterface<TKey,T,TIdentifier>
*/
public function getPreviousPage(): ?PagerItemInterface;

/**
* Gets n next pages
*
* @param int<1,max> $numberOfPages
* @return array<int,PagerItemInterface<TKey,T,TIdentifier>>
*/
public function getNextPages(int $numberOfPages): array;

/**
* Gets n previous pages
*
* @param int<1,max> $numberOfPages
* @return array<int,PagerItemInterface<TKey,T,TIdentifier>>
*/
public function getPreviousPages(int $numberOfPages): array;
}
62 changes: 50 additions & 12 deletions packages/rekapager-core/src/Pager/Internal/PagerItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<TKey2,T2,TIdentifier2> $page
* @return PagerItem<TKey2,T2,TIdentifier2>
*/
private function decorate(PageInterface $page): PagerItem
{
if ($page instanceof PagerItem) {
return $page;
}

return new PagerItem(
wrapped: $page,
pagerUrlGenerator: $this->pagerUrlGenerator,
);
}
}

/**
Expand Down

0 comments on commit 517a858

Please sign in to comment.