Skip to content
Permalink
Browse files

Addinig serializable support for ZipIterator and documenting new func…

…tions
  • Loading branch information...
lorenzo committed Jul 7, 2015
1 parent 5ded2c0 commit 28fc8cc30ba92ba7cd9e1eb0e7664f348d0b2da4
@@ -50,11 +50,23 @@ public function __construct($items)
parent::__construct($items);
}
/**
* Returns a string representation of this object that can be used
* to reconstruct it
*
* @return string
*/
public function serialize()
{
return serialize($this->buffered());
}
/**
* Unserializes the passed string and rebuilds the Collection instance
*
* @param string $collection The serialized collection
* @return void
*/
public function unserialize($collection)
{
$this->__construct(unserialize($collection));
@@ -178,6 +178,12 @@ public function count()
return $this->_buffer->count();
}
/**
* Returns a string representation of this object that can be used
* to reconstruct it
*
* @return string
*/
public function serialize()
{
if (!$this->_finished) {
@@ -187,6 +193,12 @@ public function serialize()
return serialize($this->_buffer);
}
/**
* Unserializes the passed string and rebuilds the BufferedIterator instance
*
* @param string $buffer The serialized buffer iterator
* @return void
*/
public function unserialize($buffer)
{
$this->__construct([]);
@@ -18,6 +18,7 @@
use Cake\Collection\CollectionInterface;
use Cake\Collection\CollectionTrait;
use MultipleIterator;
use Serializable;
/**
* Creates an iterator that returns elements grouped in pairs
@@ -42,7 +43,7 @@
* ```
*
*/
class ZipIterator extends MultipleIterator implements CollectionInterface
class ZipIterator extends MultipleIterator implements CollectionInterface, Serializable
{
use CollectionTrait;
@@ -54,6 +55,13 @@ class ZipIterator extends MultipleIterator implements CollectionInterface
*/
protected $_callback;
/**
* Contains the original iterator objects that were attached
*
* @var array
*/
protected $_iterators = [];
/**
* Creates the iterator to merge together the values by for all the passed
* iterators by their corresponding index.
@@ -71,6 +79,7 @@ public function __construct(array $sets, $callable = null)
parent::__construct(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC);
foreach ($sets as $set) {
$this->_iterators[] = $set;
$this->attachIterator($set);
}
}
@@ -89,4 +98,30 @@ public function current()
return call_user_func_array($this->_callback, parent::current());
}
/**
* Returns a string representation of this object that can be used
* to reconstruct it
*
* @return string
*/
public function serialize()
{
return serialize($this->_iterators);
}
/**
* Unserializes the passed string and rebuilds the ZipIterator instance
*
* @param string $iterators The serialized iterators
* @return void
*/
public function unserialize($iterators)
{
parent::__construct(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC);
$this->_iterators = unserialize($iterators);
foreach ($this->_iterators as $it) {
$this->attachIterator($it);
}
}
}
@@ -1447,4 +1447,55 @@ public function testSerializeSimpleCollection()
$this->assertEquals($collection->toList(), $unserialized->toList());
$this->assertEquals($collection->toArray(), $unserialized->toArray());
}
/**
* Tests serialization when using append
*
* @return void
*/
public function testSerializeWithAppendIterators()
{
$collection = new Collection([1, 2, 3]);
$collection = $collection->append(['a' => 4, 'b' => 5, 'c' => 6]);
$selialized = serialize($collection);
$unserialized = unserialize($selialized);
$this->assertEquals($collection->toList(), $unserialized->toList());
$this->assertEquals($collection->toArray(), $unserialized->toArray());
}
/**
* Tests serialization when using append
*
* @return void
*/
public function testSerializeWithNestedIterators()
{
$collection = new Collection([1, 2, 3]);
$collection = $collection->map(function ($e) {
return $e * 3;
});
$collection = $collection->groupBy(function ($e) {
return $e % 2;
});
$selialized = serialize($collection);
$unserialized = unserialize($selialized);
$this->assertEquals($collection->toList(), $unserialized->toList());
$this->assertEquals($collection->toArray(), $unserialized->toArray());
}
/**
* Tests serializing a zip() call
*
* @return void
*/
public function testSerializeWithZipIterator()
{
$collection = new Collection([4, 5]);
$collection = $collection->zip([1, 2]);
$selialized = serialize($collection);
$unserialized = unserialize($selialized);
$this->assertEquals($collection->toList(), $unserialized->toList());
}
}

0 comments on commit 28fc8cc

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