Skip to content

Commit

Permalink
Support first result greater than total items on paginator array adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric GELOEN committed Nov 9, 2024
1 parent fc8fa00 commit 6ae1dec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/State/Pagination/ArrayPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/State/Pagination/ArrayPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}

0 comments on commit 6ae1dec

Please sign in to comment.