From 96955d7f0cbdb0174de58607749cc050d0588df9 Mon Sep 17 00:00:00 2001 From: Dick van der Heiden Date: Mon, 6 Nov 2017 10:31:22 +0100 Subject: [PATCH] Fix bug for first item on page for first page --- src/Pagination.php | 10 +++++++++- tests/unit/PaginationTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Pagination.php b/src/Pagination.php index 6b738d0..90f54ad 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -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); } diff --git a/tests/unit/PaginationTest.php b/tests/unit/PaginationTest.php index afc4450..4c0f3ad 100644 --- a/tests/unit/PaginationTest.php +++ b/tests/unit/PaginationTest.php @@ -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()