Skip to content

Commit aa1667d

Browse files
GeLoLabsEric GELOEN
andauthored
fix(state): empty result when the array paginator is out of bound (#6785)
When using Doctrine or Elastic paginator, it behaves by giving an empty array when the page is out of bounds but when using the Array paginator, it throws an OutOfBoundsException. This PR align this behavior accross all paginators. Co-authored-by: Eric GELOEN <[email protected]>
1 parent bba0306 commit aa1667d

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/State/Pagination/ArrayPaginator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ final class ArrayPaginator implements \IteratorAggregate, PaginatorInterface, Ha
2727

2828
public function __construct(array $results, int $firstResult, int $maxResults)
2929
{
30-
if ($maxResults > 0) {
30+
$this->firstResult = $firstResult;
31+
$this->maxResults = $maxResults;
32+
$this->totalItems = \count($results);
33+
34+
if ($maxResults > 0 && $firstResult < $this->totalItems) {
3135
$this->iterator = new \LimitIterator(new \ArrayIterator($results), $firstResult, $maxResults);
3236
} else {
3337
$this->iterator = new \EmptyIterator();
3438
}
35-
$this->firstResult = $firstResult;
36-
$this->maxResults = $maxResults;
37-
$this->totalItems = \count($results);
3839
}
3940

4041
/**

tests/State/Pagination/ArrayPaginatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static function initializeProvider(): array
4343
'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],
4444
'Empty results' => [[], 0, 2, 0, 0, 1, 1, false],
4545
'0 for max results' => [[0, 1, 2, 3], 2, 0, 0, 4, 1, 1, false],
46+
'First result greater than total items' => [[0, 1], 2, 1, 0, 2, 3, 2, false],
4647
];
4748
}
4849
}

0 commit comments

Comments
 (0)