Skip to content

Commit

Permalink
Addinig serializable support for ZipIterator and documenting new func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
lorenzo committed Jul 7, 2015
1 parent 5ded2c0 commit 28fc8cc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Collection/Collection.php
Expand Up @@ -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));
Expand Down
12 changes: 12 additions & 0 deletions src/Collection/Iterator/BufferedIterator.php
Expand Up @@ -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) {
Expand All @@ -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([]);
Expand Down
37 changes: 36 additions & 1 deletion src/Collection/Iterator/ZipIterator.php
Expand Up @@ -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
Expand All @@ -42,7 +43,7 @@
* ```
*
*/
class ZipIterator extends MultipleIterator implements CollectionInterface
class ZipIterator extends MultipleIterator implements CollectionInterface, Serializable
{

use CollectionTrait;
Expand All @@ -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.
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
}
51 changes: 51 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -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.