Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset support for Pagerfanta #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion src/Pagerfanta/Pagerfanta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -323,7 +324,7 @@ private function getCurrentPageResultsFromAdapter()

private function calculateOffsetForCurrentPageResults()
{
return ($this->getCurrentPage() - 1) * $this->getMaxPerPage();
return (($this->getCurrentPage() - 1) * $this->getMaxPerPage()) + $this->getOffset();
}

/**
Expand Down Expand Up @@ -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;
}
}
103 changes: 103 additions & 0 deletions tests/Pagerfanta/Tests/PagerfantaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Pagerfanta\Tests;

use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,7 +24,11 @@ public function getIterator()

class PagerfantaTest extends TestCase
{
/**
* @var AdapterInterface
*/
private $adapter;

/**
* @var Pagerfanta
*/
Expand Down Expand Up @@ -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',
),
),
);
}
}