Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Fix bug for first item on page for first page
Browse files Browse the repository at this point in the history
  • Loading branch information
Dick van der Heiden committed Nov 6, 2017
1 parent 1969219 commit 96955d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,16 @@ private function calculateOffset()
*/
private function calculateFirstItemOnPage()
{
// When an offset is used, increase the first item with 1
$offsetItem = $this->getOffset() !== 0 ? 1 : 0;

// When there are a total amount of items and the offset is zero, increase 1
if ($this->getTotalItems() !== 0 && $offsetItem === 0) {
$offsetItem = 1;
}

// Determine the first item on the page
$firstItem = $this->getOffset() + ($this->getOffset() !== 0 ? 1 : 0);
$firstItem = $this->getOffset() + $offsetItem;

$this->setFirstItemOnPage((int)$firstItem);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public function testPaginationFirstPageWithoutTotalItems()
public function testPaginationFirstPageWithTotalItems()
{
$limit = 25;
$page = 2;
$page = 1;
$totalItems = 54;

$pagination = new Pagination($limit, $page, $totalItems);
$this->assertSame($limit, $pagination->getLimit());
$this->assertTrue($pagination->hasLimit());
$this->assertSame(25, $pagination->getOffset());
$this->assertSame(0, $pagination->getOffset());
$this->assertSame($totalItems, $pagination->getTotalItems());
$this->assertSame($page, $pagination->getPage());
$this->assertSame(3, $pagination->getTotalPages());
$this->assertSame(0, count($pagination->getParameters()));
$this->assertSame(26, $pagination->getFirstItemOnPage());
$this->assertSame(50, $pagination->getLastItemOnPage());
$this->assertSame(1, $pagination->getFirstItemOnPage());
$this->assertSame(25, $pagination->getLastItemOnPage());
}

public function testPaginationSecondPageWithoutTotalItems()
Expand Down

0 comments on commit 96955d7

Please sign in to comment.