Skip to content

Commit 1b01d76

Browse files
author
Eric GELOEN
committed
Merge branch 'array-paginator-out-of-bounds' into 4.0-fixed
2 parents 417fef5 + a184a73 commit 1b01d76

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
@@ -41,6 +41,7 @@ public static function initializeProvider(): array
4141
'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],
4242
'Empty results' => [[], 0, 2, 0, 0, 1, 1, false],
4343
'0 for max results' => [[0, 1, 2, 3], 2, 0, 0, 4, 1, 1, false],
44+
'First result greater than total items' => [[0, 1], 2, 1, 0, 2, 3, 2, false],
4445
];
4546
}
4647
}

0 commit comments

Comments
 (0)