Table of Contents

class CallIterator

Prototype

class CallIterator implements Iterator
{
    public void __construct(array|Traversable $iterator, string $method, scalar|array $method_arguments = array());
    public void setMethod(string $method);
    public string getMethod();
    public void setMethodArguments(scalar|array $arguments);
    public array getMethodArguments();
    [ ... Interface method ... ]
}

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}();
    }
}