From 36a7faa1fe8a4a4cc8f75b549eccc3abc8990e42 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Wed, 31 Jan 2024 20:40:22 +1000 Subject: [PATCH] Support paginations aware of their last page --- src/Dtos/Config.php | 2 +- src/LazyJsonPages.php | 4 +-- src/Paginations/AnyPagination.php | 2 +- src/Paginations/LastPageAwarePagination.php | 33 +++++++++++++++++++ src/Paginations/TotalItemsAwarePagination.php | 2 +- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 src/Paginations/LastPageAwarePagination.php diff --git a/src/Dtos/Config.php b/src/Dtos/Config.php index 29244b8..667d8ef 100644 --- a/src/Dtos/Config.php +++ b/src/Dtos/Config.php @@ -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, diff --git a/src/LazyJsonPages.php b/src/LazyJsonPages.php index c7307e7..df99caf 100644 --- a/src/LazyJsonPages.php +++ b/src/LazyJsonPages.php @@ -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; } diff --git a/src/Paginations/AnyPagination.php b/src/Paginations/AnyPagination.php index dd56b4e..1af0cbe 100644 --- a/src/Paginations/AnyPagination.php +++ b/src/Paginations/AnyPagination.php @@ -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, diff --git a/src/Paginations/LastPageAwarePagination.php b/src/Paginations/LastPageAwarePagination.php new file mode 100644 index 0000000..a764f23 --- /dev/null +++ b/src/Paginations/LastPageAwarePagination.php @@ -0,0 +1,33 @@ +config->lastPageKey !== null; + } + + /** + * Yield the paginated items. + * + * @return Traversable + */ + public function getIterator(): Traversable + { + yield from $this->yieldItemsUntilKey($this->config->lastPageKey, function (int $page) { + return $this->config->firstPage === 0 ? $page + 1 : $page; + }); + } +} diff --git a/src/Paginations/TotalItemsAwarePagination.php b/src/Paginations/TotalItemsAwarePagination.php index 04977e6..21087c8 100644 --- a/src/Paginations/TotalItemsAwarePagination.php +++ b/src/Paginations/TotalItemsAwarePagination.php @@ -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; } /**