Skip to content

ConsecutiveGroupIterator

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

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 exposed by the inner iterator.

public count(): int

If the inner iterator implements

  • See: \Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an

  • See: \IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return Value:

the total number of elements available from the inner iterator


Clone this wiki locally