Skip to content
Permalink
Browse files

Making BufferedIterator Countable as an optimization for the ORM

  • Loading branch information...
lorenzo committed Sep 22, 2014
1 parent cefcfcf commit 523020c5c4860b374b19f2d8be28e818cd0927c6
Showing with 22 additions and 1 deletion.
  1. +22 −1 src/Collection/Iterator/BufferedIterator.php
@@ -15,13 +15,14 @@
namespace Cake\Collection\Iterator;
use Cake\Collection\Collection;
use Countable;
use SplDoublyLinkedList;
/**
* Creates an iterator from another iterator that will keep the results of the inner
* iterator in memory, so that results don't have to be re-calculated.
*/
class BufferedIterator extends Collection {
class BufferedIterator extends Collection implements Countable {
/**
* The in-memory cache containing results from previous iterators
@@ -151,4 +152,24 @@ public function next() {
}
}
/**
* Returns the number or items in this collection
*
* @return int
*/
public function count() {
if ($this->getInnerIterator() instanceof Countable) {
return $this->getInnerIterator()->count();
}
if (!$this->_started) {
$this->rewind();
}
while ($this->valid()) {
$this->next();
}
return $this->_buffer->count();
}
}

0 comments on commit 523020c

Please sign in to comment.
You can’t perform that action at this time.