diff --git a/src/Pagerfanta/Pagerfanta.php b/src/Pagerfanta/Pagerfanta.php index 19d208ca..fdf882b6 100644 --- a/src/Pagerfanta/Pagerfanta.php +++ b/src/Pagerfanta/Pagerfanta.php @@ -36,6 +36,7 @@ class Pagerfanta implements \Countable, \IteratorAggregate, \JsonSerializable, P private $currentPage; private $nbResults; private $currentPageResults; + private $offset = 0; /** * @param AdapterInterface $adapter An adapter. @@ -323,7 +324,7 @@ private function getCurrentPageResultsFromAdapter() private function calculateOffsetForCurrentPageResults() { - return ($this->getCurrentPage() - 1) * $this->getMaxPerPage(); + return (($this->getCurrentPage() - 1) * $this->getMaxPerPage()) + $this->getOffset(); } /** @@ -539,4 +540,24 @@ public function getPageNumberForItemAtPosition($position) return (int) ceil($position/$this->getMaxPerPage()); } + + /** + * @return integer + */ + public function getOffset() + { + return $this->offset; + } + + /** + * @param integer $offset + * + * @return Pagerfanta + */ + public function setOffset($offset) + { + $this->offset = $offset; + + return $this; + } } diff --git a/tests/Pagerfanta/Tests/PagerfantaTest.php b/tests/Pagerfanta/Tests/PagerfantaTest.php index ba24ed1d..5ab12cce 100644 --- a/tests/Pagerfanta/Tests/PagerfantaTest.php +++ b/tests/Pagerfanta/Tests/PagerfantaTest.php @@ -2,6 +2,8 @@ namespace Pagerfanta\Tests; +use Pagerfanta\Adapter\AdapterInterface; +use Pagerfanta\Adapter\ArrayAdapter; use Pagerfanta\Pagerfanta; use PHPUnit\Framework\TestCase; @@ -22,7 +24,11 @@ public function getIterator() class PagerfantaTest extends TestCase { + /** + * @var AdapterInterface + */ private $adapter; + /** * @var Pagerfanta */ @@ -757,4 +763,101 @@ public function testGetPageNumberForItemShouldThrowALogicExceptionIfTheItemIsMor $this->pagerfanta->getPageNumberForItemAtPosition(101); } + + /** + * @dataProvider dataProviderTestOffset + */ + public function testOffset($offset, $limit, $page, $expectedResult) + { + $currentPageResults = array( + 'item 1', + 'item 2', + 'item 3', + 'item 4', + 'item 5', + 'item 6', + 'item 7', + 'item 8', + 'item 9', + 'item 10', + 'item 11', + 'item 12', + 'item 13', + 'item 14', + 'item 15', + ); + + $adapter = new ArrayAdapter($currentPageResults); + $pagerfanta = new Pagerfanta($adapter); + + $pagerfanta->setOffset($offset); + $pagerfanta->setMaxPerPage($limit); + $pagerfanta->setCurrentPage($page); + $this->assertEquals($expectedResult, $pagerfanta->getCurrentPageResults()); + } + + public function dataProviderTestOffset() + { + return array( + 'Page 1, offset 5, limit 5' => array( + 'offset' => 5, + 'limit' => 5, + 'page' => 1, + 'expectedResult' => array( + 'item 6', + 'item 7', + 'item 8', + 'item 9', + 'item 10', + ), + ), + 'Page 2, offset 5, limit 5' => array( + 'offset' => 5, + 'limit' => 5, + 'page' => 2, + 'expectedResult' => array( + 'item 11', + 'item 12', + 'item 13', + 'item 14', + 'item 15', + ), + ), + 'Page 3, offset 5, limit 5' => array( + 'offset' => 5, + 'limit' => 5, + 'page' => 3, + 'expectedResult' => array(), + ), + 'offset 0, page 1' => array( + 'offset' => 0, + 'limit' => 10, + 'page' => 1, + 'expectedResult' => array( + 'item 1', + 'item 2', + 'item 3', + 'item 4', + 'item 5', + 'item 6', + 'item 7', + 'item 8', + 'item 9', + 'item 10', + ), + ), + 'offset 0, page 2' => array( + 'offset' => 0, + 'limit' => 10, + 'page' => 2, + 'expectedResult' => array( + 'item 11', + 'item 12', + 'item 13', + 'item 14', + 'item 15', + ), + ), + ); + } }