rfc:functional-elements:call-iterator
This is an old revision of the document!
class CallIterator
Prototype
class CallIterator implements Iterator { void public function __construct(array|Traversable $iterator, string $method, array $method_arguments = array()); void public function setMethod(string $method); string public function getMethod(); void public function setMethodArguments(scalar|array $arguments); array getMethodArguments(); [ ... Interface implementations ... ] }
Simplified example implementation
class CallIterator implements Iterator { protected $method; protected $iterator; public function __construct($iterator, $method) { $this->method = $method; if (!is_traversable($iterator)) { throw new InvalidArgumentException(); } if ($iterator instanceof IteratorAggregate) { $this->iterator = $iterator->getIterator(); } elseif ($iterator instanceof Iterator) { $this->iterator = $iterator; } else { $this->iterator = new ArrayIterator($iterator); } } public function next() { $this->iterator->next(); } public function key() { return $this->iterator->key(); } public function rewind() { $this->iterator->rewind(); } public function valid() { return $this->iterator->valid(); } public function current() { if (!is_callable(array($this->iterator->current(), $this->method))) { throw new RuntimeException(); } return $this->iterator->current()->{$this->method}(); } }
rfc/functional-elements/call-iterator.1205693772.txt.gz · Last modified: 2017/09/22 13:28 (external edit)