Skip to content
Permalink
Browse files

Added Collection::buffered() to make any non-rewidable rewindable

  • Loading branch information...
lorenzo committed Sep 22, 2014
1 parent 8118c7e commit a73697246dd918472702f121e036f41e7bfde7d9
Showing with 29 additions and 0 deletions.
  1. +14 −0 src/Collection/CollectionTrait.php
  2. +15 −0 tests/TestCase/Collection/CollectionTest.php
@@ -17,6 +17,7 @@
use AppendIterator;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\Collection\Iterator\BufferedIterator;
use Cake\Collection\Iterator\ExtractIterator;
use Cake\Collection\Iterator\FilterIterator;
use Cake\Collection\Iterator\InsertIterator;
@@ -889,6 +890,19 @@ public function compile($preserveKeys = true) {
return new Collection($this->toArray($preserveKeys));
}
/**
* Returns a new collection where the operations performed by this collection.
* Not matter how many times the new collection is iterated, those operations will
* only be performed once.
*
* This can also be used to make any non-rewindable iterator rewindable.
*
* @return \Cake\Collection\Iterator\BufferedIterator
*/
public function buffered() {
return new BufferedIterator($this);
}
/**
* Returns a new collection with each of the elements of this collection
* after flattening the tree structure. The tree structure is defined
@@ -14,9 +14,11 @@
*/
namespace Cake\Test\TestCase\Collection;
use ArrayIterator;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\TestSuite\TestCase;
use NoRewindIterator;
/**
* CollectionTest
@@ -645,6 +647,19 @@ public function testCompile() {
$this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
}
/**
* Tests converting a non rewindable iterator into a rewindable one using
* the buffered method.
*
* @return void
*/
public function testBuffered() {
$items = new NoRewindIterator(new ArrayIterator(['a' => 4, 'b' => 5, 'c' => 6]));
$buffered = (new Collection($items))->buffered();
$this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
$this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
}
/**
* Tests the combine method
*

0 comments on commit a736972

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