Skip to content

ConsecutiveGroupIterator

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

Class ConsecutiveGroupIterator.

Groups elements dynamically based on a user-defined condition.

This iterator chunks elements from a traversable source, creating groups where each new element is added to the current chunk until the provided callback returns false, signaling the start of a new chunk.

Usage Example:


Methods

__construct

Initializes the ConsecutiveGroupIterator.

public __construct(iterable $iterator, \Closure $callback): mixed

Parameters:

Parameter Type Description
$iterator iterable the iterator containing values to be chunked
$callback \Closure The function that determines whether elements should be in the same chunk.
It receives two arguments: $previous and $current,
and must return true to keep them together or false to start a new chunk.

current

Retrieves the current chunk of elements.

public current(): array<int,mixed>

Return Value:

the current chunk as an array


key

Retrieves the current group key.

public key(): int|null

Return Value:

the current group key, or null if the iterator is not valid


next

Advances to the next chunk of elements.

public next(): void

valid

Checks if the current chunk contains valid elements.

public valid(): bool

Return Value:

true if a chunk exists, false otherwise


rewind

Resets the iterator and prepares the first chunk.

public rewind(): void

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