Skip to content

Commit

Permalink
reverted the iterator to array convertion in the ->getCurrentPageResu…
Browse files Browse the repository at this point in the history
…lts() method
  • Loading branch information
pablodip committed Dec 15, 2011
1 parent 94bd483 commit a061ad2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
37 changes: 14 additions & 23 deletions src/Pagerfanta/Pagerfanta.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Pagerfanta implements PagerfantaInterface
private $maxPerPage;
private $currentPage;
private $nbResults;
private $slice;
private $currentPageResults;
private $nbPages;

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ public function setMaxPerPage($maxPerPage)
throw new LessThan1MaxPerPageException();
}

$this->slice = null;
$this->currentPageResults = null;
$this->nbPages = null;
$this->maxPerPage = $maxPerPage;

Expand Down Expand Up @@ -130,7 +130,7 @@ public function setCurrentPage($currentPage, $allowOutOfRangePages = false, $nor
}
}

$this->slice = null;
$this->currentPageResults = null;
$this->currentPage = $currentPage;

return $this;
Expand All @@ -149,13 +149,13 @@ public function getCurrentPage()
*/
public function getCurrentPageResults()
{
$this->fetchSlice();

if ($this->slice instanceof \Iterator || $this->slice instanceof \IteratorAggregate) {
return iterator_to_array($this->slice);
if (null === $this->currentPageResults) {
$offset = ($this->getCurrentPage() - 1) * $this->getMaxPerPage();
$length = $this->getMaxPerPage();
$this->currentPageResults = $this->adapter->getSlice($offset, $length);
}

return $this->slice;
return $this->currentPageResults;
}

/**
Expand Down Expand Up @@ -251,25 +251,16 @@ public function count()
*/
public function getIterator()
{
$this->fetchSlice();
$currentPageResults = $this->getCurrentPageResults();

if ($this->slice instanceof \Iterator) {
return $this->slice;
if ($currentPageResults instanceof \Iterator) {
return $currentPageResults;
}

if ($this->slice instanceof \IteratorAggregate) {
return $this->slice->getIterator();
if ($currentPageResults instanceof \IteratorAggregate) {
return $currentPageResults->getIterator();
}

return new \ArrayIterator($this->slice);
}

private function fetchSlice()
{
if (null === $this->slice) {
$offset = ($this->getCurrentPage() - 1) * $this->getMaxPerPage();
$length = $this->getMaxPerPage();
$this->slice = $this->adapter->getSlice($offset, $length);
}
return new \ArrayIterator($currentPageResults);
}
}
2 changes: 1 addition & 1 deletion src/Pagerfanta/PagerfantaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getCurrentPage();
/**
* Returns the results for the current page.
*
* @return array The results.
* @return array|\Traversable The results.
*
* @api
*/
Expand Down
27 changes: 0 additions & 27 deletions tests/Pagerfanta/Tests/PagerfantaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,6 @@ public function testGetCurrentPageResults()
$this->assertSame($returnValues[0], $this->pagerfanta->getCurrentPageResults());
}

public function testGetCurrentPageResultsIteratorToArray()
{
$array = array('foo', 'bar', 'ups');
$iterator = new \ArrayIterator($array);

$this->adapter
->expects($this->once())
->method('getSlice')
->will($this->returnValue($iterator))
;

$this->assertSame($array, $this->pagerfanta->getCurrentPageResults());
}

public function testGetCurrentPageResultsIteratorAggregateToArray()
{
$iteratorAggregate = new IteratorAggregate();

$this->adapter
->expects($this->once())
->method('getSlice')
->will($this->returnValue($iteratorAggregate))
;

$this->assertSame(iterator_to_array($iteratorAggregate), $this->pagerfanta->getCurrentPageResults());
}

public function testGetNbResults()
{
$this->adapter
Expand Down

0 comments on commit a061ad2

Please sign in to comment.