_callback = $callback; parent::__construct($items); $this->_innerIterator = $this->getInnerIterator(); } /** * Returns the value returned by the callback after passing the current value in * the iteration * * @return mixed */ public function current() { $callback = $this->_callback; return $callback(parent::current(), $this->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->_callback; $res = []; foreach ($iterator as $k => $v) { $res[$k] = $callback($v, $k, $iterator); } return new ArrayIterator($res); } }