= $n) { throw new Exception( 'n must be greater than 0, given %d.', 0, $n ); } if ($iterator instanceof \IteratorAggregate) { $iterator = $iterator->getIterator(); } $this->_iterator = $iterator; $this->_n = $n; $this->_body = $body; return; } /** * Return the current element. * * @return mixed */ public function current() { return $this->_iterator->current(); } /** * Return the key of the current element. * * @return mixed */ public function key() { return $this->_iterator->key(); } /** * Move forward to next element. * * @return void */ public function next() { return $this->_iterator->next(); } /** * Rewind the iterator to the first element. * * @return void */ public function rewind() { return $this->_iterator->rewind(); } /** * Check if current position is valid. * * @return bool */ public function valid() { $valid = $this->_iterator->valid(); if (true === $valid) { return true; } if (null !== $this->_body) { $handle = &$this->_body; $handle($this->_i); } $this->rewind(); if ($this->_n <= $this->_i++) { $this->_i = 1; return false; } return true; } }