Skip to content

Commit 34a15c4

Browse files
committed
Lazy adapters implementation
1 parent 0eec60b commit 34a15c4

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

src/Pagerfanta/Adapter/ElasticaAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Elastica\Query;
1515
use Elastica\SearchableInterface;
1616

17-
class ElasticaAdapter implements AdapterInterface
17+
class ElasticaAdapter implements LazyAdapterInterface
1818
{
1919
/**
2020
* @var Query
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Pagerfanta package.
5+
*
6+
* (c) Pablo Díez <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Pagerfanta\Adapter;
13+
14+
/**
15+
* LazyAdapterInterface that marks adapter lazy.
16+
*
17+
* @author Konstantin Myakshin <[email protected]>
18+
*/
19+
interface LazyAdapterInterface extends AdapterInterface
20+
{
21+
22+
}

src/Pagerfanta/Pagerfanta.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Pagerfanta;
1313

1414
use Pagerfanta\Adapter\AdapterInterface;
15+
use Pagerfanta\Adapter\LazyAdapterInterface;
1516
use Pagerfanta\Exception\LogicException;
1617
use Pagerfanta\Exception\NotBooleanException;
1718
use Pagerfanta\Exception\NotIntegerMaxPerPageException;
@@ -225,7 +226,9 @@ private function filterCurrentPage($currentPage)
225226
{
226227
$currentPage = $this->toInteger($currentPage);
227228
$this->checkCurrentPage($currentPage);
228-
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
229+
if (!$this->adapter instanceof LazyAdapterInterface) {
230+
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
231+
}
229232

230233
return $currentPage;
231234
}
@@ -316,7 +319,18 @@ private function getCurrentPageResultsFromAdapter()
316319
$offset = $this->calculateOffsetForCurrentPageResults();
317320
$length = $this->getMaxPerPage();
318321

319-
return $this->adapter->getSlice($offset, $length);
322+
$slice = $this->adapter->getSlice($offset, $length);
323+
324+
if ($this->adapter instanceof LazyAdapterInterface) {
325+
$page = $this->filterOutOfRangeCurrentPage($this->getCurrentPage());
326+
if ($page != $this->getCurrentPage()) {
327+
$this->setCurrentPage($page);
328+
329+
return $this->getCurrentPageResultsFromAdapter();
330+
}
331+
}
332+
333+
return $slice;
320334
}
321335

322336
private function calculateOffsetForCurrentPageResults()

tests/Pagerfanta/Tests/PagerfantaTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,42 @@ public function testPagerfantaShouldImplementIteratorAggregateInterface()
671671
$this->assertInstanceOf('IteratorAggregate', $this->pagerfanta);
672672
}
673673

674+
public function testLazyAdapterDoNotCallsGetNbResultsOnSetCurrentPage()
675+
{
676+
$adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');
677+
678+
$adapter
679+
->expects($this->never())
680+
->method('getNbResults');
681+
682+
$pagerfanta = new Pagerfanta($adapter);
683+
$pagerfanta->setMaxPerPage(10);
684+
$pagerfanta->setCurrentPage(3);
685+
}
686+
687+
/**
688+
* @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException
689+
*/
690+
public function testLazyAdapterCallsGetNbResultsOnGetCurrentPageResults()
691+
{
692+
$adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');
693+
694+
$adapter
695+
->expects($this->any())
696+
->method('getSlice')
697+
->will($this->returnValue(range(0, 9)));
698+
699+
$adapter
700+
->expects($this->any())
701+
->method('getNbResults')
702+
->will($this->returnValue(20));
703+
704+
$pagerfanta = new Pagerfanta($adapter);
705+
$pagerfanta->setMaxPerPage(10);
706+
$pagerfanta->setCurrentPage(3);
707+
$pagerfanta->getCurrentPageResults();
708+
}
709+
674710
private function assertResetCurrentPageResults($callback)
675711
{
676712
$this->setAdapterNbResultsAny(100);

0 commit comments

Comments
 (0)