Skip to content

Commit

Permalink
Lazy adapters implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Feb 21, 2017
1 parent 3af44a4 commit 772d273
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Pagerfanta/Adapter/ElasticaAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Elastica\Query;
use Elastica\SearchableInterface;

class ElasticaAdapter implements AdapterInterface
class ElasticaAdapter implements LazyAdapterInterface
{
/**
* @var Query
Expand Down
22 changes: 22 additions & 0 deletions src/Pagerfanta/Adapter/LazyAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pagerfanta\Adapter;

/**
* LazyAdapterInterface that marks adapter lazy.
*
* @author Konstantin Myakshin <[email protected]>
*/
interface LazyAdapterInterface extends AdapterInterface
{

}
18 changes: 16 additions & 2 deletions src/Pagerfanta/Pagerfanta.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Pagerfanta;

use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Adapter\LazyAdapterInterface;
use Pagerfanta\Exception\LogicException;
use Pagerfanta\Exception\NotBooleanException;
use Pagerfanta\Exception\NotIntegerMaxPerPageException;
Expand Down Expand Up @@ -225,7 +226,9 @@ private function filterCurrentPage($currentPage)
{
$currentPage = $this->toInteger($currentPage);
$this->checkCurrentPage($currentPage);
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
if (!$this->adapter instanceof LazyAdapterInterface) {
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
}

return $currentPage;
}
Expand Down Expand Up @@ -316,7 +319,18 @@ private function getCurrentPageResultsFromAdapter()
$offset = $this->calculateOffsetForCurrentPageResults();
$length = $this->getMaxPerPage();

return $this->adapter->getSlice($offset, $length);
$slice = $this->adapter->getSlice($offset, $length);

if ($this->adapter instanceof LazyAdapterInterface) {
$page = $this->filterOutOfRangeCurrentPage($this->getCurrentPage());
if ($page != $this->getCurrentPage()) {
$this->setCurrentPage($page);

return $this->getCurrentPageResultsFromAdapter();
}
}

return $slice;
}

private function calculateOffsetForCurrentPageResults()
Expand Down
36 changes: 36 additions & 0 deletions tests/Pagerfanta/Tests/PagerfantaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,42 @@ public function testPagerfantaShouldImplementIteratorAggregateInterface()
$this->assertInstanceOf('IteratorAggregate', $this->pagerfanta);
}

public function testLazyAdapterDoNotCallsGetNbResultsOnSetCurrentPage()
{
$adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');

$adapter
->expects($this->never())
->method('getNbResults');

$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(10);
$pagerfanta->setCurrentPage(3);
}

/**
* @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException
*/
public function testLazyAdapterCallsGetNbResultsOnGetCurrentPageResults()
{
$adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');

$adapter
->expects($this->any())
->method('getSlice')
->will($this->returnValue(range(0, 9)));

$adapter
->expects($this->any())
->method('getNbResults')
->will($this->returnValue(20));

$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(10);
$pagerfanta->setCurrentPage(3);
$pagerfanta->getCurrentPageResults();
}

private function assertResetCurrentPageResults($callback)
{
$this->setAdapterNbResultsAny(100);
Expand Down

0 comments on commit 772d273

Please sign in to comment.