_condition = $condition; parent::__construct($items); $this->_innerIterator = $this->getInnerIterator(); } /** * Evaluates the condition and returns its result, this controls * whether or not more results will be yielded. * * @return bool */ public function valid() { if (!parent::valid()) { return false; } $current = $this->current(); $key = $this->key(); $condition = $this->_condition; return !$condition($current, $key, $this->_innerIterator); } /** * {@inheritDoc} * * We perform here some strictness analysis so that the * iterator logic is bypassed entirely. * * @return \Iterator */ public function unwrap() { $iterator = $this->_innerIterator; if ($iterator instanceof CollectionInterface) { $iterator = $iterator->unwrap(); } if (get_class($iterator) !== ArrayIterator::class) { return $this; } // ArrayIterator can be traversed strictly. // Let's do that for performance gains $callback = $this->_condition; $res = []; foreach ($iterator as $k => $v) { if ($callback($v, $k, $iterator)) { break; } $res[$k] = $v; } return new ArrayIterator($res); } }