Skip to content

Commit 98cbad9

Browse files
committed
Lazy adapters implementation
1 parent 0eec60b commit 98cbad9

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-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()

0 commit comments

Comments
 (0)