Skip to content

Commit 324a689

Browse files
committed
Lazy adapters implementation
1 parent b0b3583 commit 324a689

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
@@ -13,6 +13,7 @@
1313

1414
use OutOfBoundsException;
1515
use Pagerfanta\Adapter\AdapterInterface;
16+
use Pagerfanta\Adapter\LazyAdapterInterface;
1617
use Pagerfanta\Exception\LogicException;
1718
use Pagerfanta\Exception\NotBooleanException;
1819
use Pagerfanta\Exception\NotIntegerException;
@@ -227,7 +228,9 @@ private function filterCurrentPage($currentPage)
227228
{
228229
$currentPage = $this->toInteger($currentPage);
229230
$this->checkCurrentPage($currentPage);
230-
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
231+
if (!$this->adapter instanceof LazyAdapterInterface) {
232+
$currentPage = $this->filterOutOfRangeCurrentPage($currentPage);
233+
}
231234

232235
return $currentPage;
233236
}
@@ -318,7 +321,18 @@ private function getCurrentPageResultsFromAdapter()
318321
$offset = $this->calculateOffsetForCurrentPageResults();
319322
$length = $this->getMaxPerPage();
320323

321-
return $this->adapter->getSlice($offset, $length);
324+
$slice = $this->adapter->getSlice($offset, $length);
325+
326+
if ($this->adapter instanceof LazyAdapterInterface) {
327+
$page = $this->filterOutOfRangeCurrentPage($this->getCurrentPage());
328+
if ($page != $this->getCurrentPage()) {
329+
$this->setCurrentPage($page);
330+
331+
return $this->getCurrentPageResultsFromAdapter();
332+
}
333+
}
334+
335+
return $slice;
322336
}
323337

324338
private function calculateOffsetForCurrentPageResults()

tests/Pagerfanta/Tests/PagerfantaTest.php

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

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

0 commit comments

Comments
 (0)