Skip to content

Instantly share code, notes, and snippets.

@TheDistantSea
Last active December 20, 2015 02:09
Show Gist options
  • Select an option

  • Save TheDistantSea/6054423 to your computer and use it in GitHub Desktop.

Select an option

Save TheDistantSea/6054423 to your computer and use it in GitHub Desktop.

Revisions

  1. TheDistantSea revised this gist Jul 22, 2013. 1 changed file with 15 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions ZipIterator.php
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,22 @@
    class ZipIterator implements \Iterator
    {
    const ZIP_ALL = '{6087F687-D674-4549-AFBF-8D4DC07FE06F}';

    private $_iterators;

    private $_zipAll = false;

    public function __construct()
    {
    foreach (func_get_args() as $traversable) {
    if (is_array($traversable)) {
    $this->_iterators[] = new \ArrayIterator($traversable);
    foreach (func_get_args() as $argument) {
    if ($argument === self::ZIP_ALL) {
    $this->_zipAll = true;
    }
    else if (is_array($argument)) {
    $this->_iterators[] = new \ArrayIterator($argument);
    }
    else if ($traversable instanceof \Traversable) {
    $this->_iterators[] = $traversable;
    else if ($argument instanceof \Traversable) {
    $this->_iterators[] = $argument;
    }
    else {
    throw new Exception("All arguments to ".__CLASS__." must be arrays or implement Traversable.");
    @@ -47,12 +54,12 @@ public function key()
    public function valid()
    {
    foreach ($this->_iterators as $iterator) {
    if ($iterator->valid()) {
    return true;
    if ($iterator->valid() ^ !$this->_zipAll) {
    return $this->_zipAll;
    }
    }

    return false;
    return !$this->_zipAll;
    }

    public function rewind()
  2. TheDistantSea created this gist Jul 22, 2013.
    64 changes: 64 additions & 0 deletions ZipIterator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    class ZipIterator implements \Iterator
    {
    private $_iterators;

    public function __construct()
    {
    foreach (func_get_args() as $traversable) {
    if (is_array($traversable)) {
    $this->_iterators[] = new \ArrayIterator($traversable);
    }
    else if ($traversable instanceof \Traversable) {
    $this->_iterators[] = $traversable;
    }
    else {
    throw new Exception("All arguments to ".__CLASS__." must be arrays or implement Traversable.");
    }
    }
    }

    public function current()
    {
    $result = [];
    foreach ($this->_iterators as $iterator) {
    $result[] = $iterator->current();
    }

    return $result;
    }

    public function next()
    {
    foreach ($this->_iterators as $iterator) {
    $iterator->next();
    }
    }

    public function key()
    {
    $result = [];
    foreach ($this->_iterators as $iterator) {
    $result[] = $iterator->key();
    }

    return $result;
    }

    public function valid()
    {
    foreach ($this->_iterators as $iterator) {
    if ($iterator->valid()) {
    return true;
    }
    }

    return false;
    }

    public function rewind()
    {
    foreach ($this->_iterators as $iterator) {
    $iterator->rewind();
    }
    }
    }