Skip to content

GeneratorCachingIteratorAggregate

github-actions edited this page Mar 31, 2026 · 3 revisions

Class GeneratorCachingIteratorAggregate.

A caching iterator aggregate designed to wrap a generator and cache its results.

This class allows wrapping a Generator or a callable returning a generator, caching its values to enable multiple iterations over the same dataset.

Usage Example:


Methods

__construct

Initializes the caching iterator with a generator or a callable returning a generator.

public __construct(callable|\Generator $generator): mixed

If a callable is provided, it is wrapped in a ClosureFactoryIteratorAggregate to generate the iterator.

Parameters:

Parameter Type Description
$generator callable|\Generator the generator or a callable returning a generator

getIterator

Retrieves the iterator, either from the cache or the generator itself.

public getIterator(): \Traversable

This method ensures that once a generator is iterated, its values remain available for subsequent iterations.

Return Value:

the cached or fresh iterator


Inherited methods

count

Counts the number of elements in the iterable.

public count(): int

If the inner iterator implements Countable, it uses that. Otherwise, it counts the elements by iterating through them.

Return Value:

the number of elements in the iterable


Clone this wiki locally