Skip to content

GeneratorRewindableIterator

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

An iterator that allows rewinding over a generator by caching its values.

This class wraps a generator or a closure returning a generator, enabling multiple iterations over the generated sequence by caching its results.

Usage Example:


Methods

__construct

Initializes the GeneratorRewindableIterator with a generator or a closure returning a generator.

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

Parameters:

Parameter Type Description
$generator \Closure|\Generator a generator instance or a callable that returns a generator

rewind

Rewinds the iterator to the beginning.

public rewind(): void

This method creates a new IteratorIterator instance wrapping the GeneratorCachingIteratorAggregate, ensuring that the generator can be reused.


current

Retrieves the current element from the iterator.

public current(): mixed

Return Value:

the current element


key

Retrieves the key of the current element.

public key(): mixed

Return Value:

the key associated with the current element


next

Advances the iterator to the next element.

public next(): void

valid

Checks if the current iterator position is valid.

public valid(): bool

Return Value:

true if the current position is valid, false otherwise


Inherited methods

count

Counts the number of elements available in the iterator.

public count(): int

This method MUST count the elements by iterating over a clone of the current iterator instance so that the active iterator state of the original object is not modified during the counting process. Concrete implementations SHOULD therefore remain safely cloneable whenever this behavior is expected to be used.

Return Value:

the total number of elements exposed by the iterator


Clone this wiki locally