Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: PagerItemInterface methods now returns itself, instead of PageInterface #2

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading