Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
];
}
}
Loading