From a184a73bdec461aa9aa3ba9bb3edb8a088cb0cb9 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Fri, 8 Nov 2024 17:00:40 +0100 Subject: [PATCH] Support first result greater than total items on paginator array adapter --- src/State/Pagination/ArrayPaginator.php | 9 +++++---- tests/State/Pagination/ArrayPaginatorTest.php | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/State/Pagination/ArrayPaginator.php b/src/State/Pagination/ArrayPaginator.php index 3de9ab6e8f1..8c50186eb62 100644 --- a/src/State/Pagination/ArrayPaginator.php +++ b/src/State/Pagination/ArrayPaginator.php @@ -27,14 +27,15 @@ final class ArrayPaginator implements \IteratorAggregate, PaginatorInterface, Ha public function __construct(array $results, int $firstResult, int $maxResults) { - if ($maxResults > 0) { + $this->firstResult = $firstResult; + $this->maxResults = $maxResults; + $this->totalItems = \count($results); + + if ($maxResults > 0 && $firstResult < $this->totalItems) { $this->iterator = new \LimitIterator(new \ArrayIterator($results), $firstResult, $maxResults); } else { $this->iterator = new \EmptyIterator(); } - $this->firstResult = $firstResult; - $this->maxResults = $maxResults; - $this->totalItems = \count($results); } /** diff --git a/tests/State/Pagination/ArrayPaginatorTest.php b/tests/State/Pagination/ArrayPaginatorTest.php index d172dd7faac..2a2744ec20b 100644 --- a/tests/State/Pagination/ArrayPaginatorTest.php +++ b/tests/State/Pagination/ArrayPaginatorTest.php @@ -43,6 +43,7 @@ public static function initializeProvider(): array 'Second of two pages of 3 items for the first page and 2 for the second' => [[0, 1, 2, 3, 4], 3, 3, 2, 5, 2, 2, false], 'Empty results' => [[], 0, 2, 0, 0, 1, 1, false], '0 for max results' => [[0, 1, 2, 3], 2, 0, 0, 4, 1, 1, false], + 'First result greater than total items' => [[0, 1], 2, 1, 0, 2, 3, 2, false], ]; } }