Skip to content

Commit

Permalink
Support paginations aware of their last page
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 31, 2024
1 parent f486a69 commit 36a7faa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Dtos/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
public readonly ?int $perPageOverride = null,
public readonly ?Closure $nextPage = null,
public readonly ?string $nextPageKey = null,
public readonly ?int $lastPage = null,
public readonly ?string $lastPageKey = null,
public readonly ?string $offsetKey = null,
public readonly ?string $pagination = null,
public readonly int $async = 3,
Expand Down
4 changes: 2 additions & 2 deletions src/LazyJsonPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ public function nextPage(Closure|string $key): self
/**
* Set the number of the last page.
*/
public function lastPage(Closure|string $key): self
public function lastPage(string $key): self
{
$this->config['lastPage'] = $this->integerFromResponse($key);
$this->config['lastPageKey'] = $key;

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Paginations/AnyPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AnyPagination extends Pagination
protected array $supportedPaginations = [
// CursorPagination::class,
CustomPagination::class,
// LastPageAwarePagination::class,
LastPageAwarePagination::class,
// LinkHeaderPagination::class,
TotalItemsAwarePagination::class,
TotalPagesAwarePagination::class,
Expand Down
33 changes: 33 additions & 0 deletions src/Paginations/LastPageAwarePagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Paginations;

use Traversable;

/**
* The pagination aware of the number of the last page.
*/
class LastPageAwarePagination extends LengthAwarePagination
{
/**
* Determine whether the configuration matches this pagination.
*/
public function matches(): bool
{
return $this->config->lastPageKey !== null;
}

/**
* Yield the paginated items.
*
* @return Traversable<int, mixed>
*/
public function getIterator(): Traversable
{
yield from $this->yieldItemsUntilKey($this->config->lastPageKey, function (int $page) {
return $this->config->firstPage === 0 ? $page + 1 : $page;
});
}
}
2 changes: 1 addition & 1 deletion src/Paginations/TotalItemsAwarePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function matches(): bool
{
return $this->config->totalItemsKey !== null
&& $this->config->totalPagesKey === null
&& $this->config->perPage === null;
&& $this->config->lastPageKey === null;
}

/**
Expand Down

0 comments on commit 36a7faa

Please sign in to comment.