Trait that implements the IteratorAggregate and Traversable interfaces. Enables traversing methods such as foreach() by aggregating a Generator.

Trait synopsis

trait IteratorAggregateTrait
{
    use TypeTrait;

    // IteratorAggregate interface implementation.

    /**
     * Iterate array and yield key/value pair.
     * @return Generator The iterator function.
     */
    public function getIterator(): Generator;
}

Examples


use Phrity\O\Array\IteratorAggregateTrait;

class MyClass implements IteratorAggregate, Traversable
{
    use IteratorAggregateTrait;

    public function __construct(array $input)
    {
        $this->initialize($input);
    }
}

$class = new MyClass(['a' => 1, 'b' => 2, 'c' => 3]);
foreach ($class as $key => $val) {
    // ...
}