diff options
Diffstat (limited to 'fusonic/linq/src/Fusonic/Linq/Iterator/IntersectIterator.php')
-rw-r--r-- | fusonic/linq/src/Fusonic/Linq/Iterator/IntersectIterator.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/fusonic/linq/src/Fusonic/Linq/Iterator/IntersectIterator.php b/fusonic/linq/src/Fusonic/Linq/Iterator/IntersectIterator.php new file mode 100644 index 00000000..d08c2efe --- /dev/null +++ b/fusonic/linq/src/Fusonic/Linq/Iterator/IntersectIterator.php @@ -0,0 +1,65 @@ +<?php + +/* + * This file is part of Fusonic-linq. + * https://github.com/fusonic/fusonic-linq + * + * (c) Fusonic GmbH + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fusonic\Linq\Iterator; + +use ArrayIterator; +use Iterator; + +class IntersectIterator implements Iterator +{ + private $first; + private $second; + private $intersections; + + public function __construct(Iterator $first, Iterator $second) + { + $this->first = $first; + $this->second = $second; + } + + public function current() + { + return $this->intersections->current(); + } + + public function next() + { + $this->intersections->next(); + } + + public function key() + { + return $this->intersections->key(); + } + + public function valid() + { + return $this->intersections->valid(); + } + + public function rewind() + { + if ($this->intersections === null) { + $this->calcIntersections(); + } + + $this->intersections->rewind(); + } + + private function calcIntersections() + { + $firstArray = iterator_to_array($this->first); + $secondArray = iterator_to_array($this->second); + $this->intersections = new ArrayIterator(array_intersect($firstArray, $secondArray)); + } +} |