Skip to content
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
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 <pablodip@gmail.com>
*
* 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 <koc-dp@yandex.ru>
*/
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 @@ -13,6 +13,7 @@

use OutOfBoundsException;
use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Adapter\LazyAdapterInterface;
use Pagerfanta\Exception\LogicException;
use Pagerfanta\Exception\NotBooleanException;
use Pagerfanta\Exception\NotIntegerException;
Expand Down Expand Up @@ -227,7 +228,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 @@ -318,7 +321,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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use a string comparison

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean (string)$page !== (string)$this->getCurrentPage()?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he probably meant strict

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I meant strict

$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 @@ -672,6 +672,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