Browse Source

Addinig serializable support for ZipIterator and documenting new functions

Jose Lorenzo Rodriguez 10 years ago
parent
commit
28fc8cc30b

+ 12 - 0
src/Collection/Collection.php

@@ -50,11 +50,23 @@ class Collection extends IteratorIterator implements CollectionInterface, Serial
         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));

+ 12 - 0
src/Collection/Iterator/BufferedIterator.php

@@ -178,6 +178,12 @@ class BufferedIterator extends Collection implements Countable, Serializable
         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 @@ class BufferedIterator extends Collection implements Countable, Serializable
         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([]);

+ 36 - 1
src/Collection/Iterator/ZipIterator.php

@@ -18,6 +18,7 @@ use Cake\Collection\Collection;
 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 @@ use MultipleIterator;
  * ```
  *
  */
-class ZipIterator extends MultipleIterator implements CollectionInterface
+class ZipIterator extends MultipleIterator implements CollectionInterface, Serializable
 {
 
     use CollectionTrait;
@@ -55,6 +56,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 @@ class ZipIterator extends MultipleIterator implements CollectionInterface
         parent::__construct(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC);
 
         foreach ($sets as $set) {
+            $this->_iterators[] = $set;
             $this->attachIterator($set);
         }
     }
@@ -89,4 +98,30 @@ class ZipIterator extends MultipleIterator implements CollectionInterface
 
         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 - 0
tests/TestCase/Collection/CollectionTest.php

@@ -1447,4 +1447,55 @@ class CollectionTest extends TestCase
         $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());
+    }
 }