-
-
Notifications
You must be signed in to change notification settings - Fork 0
ConsecutiveGroupIterator
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.
- Full name:
\FastForward\Iterator\ConsecutiveGroupIterator - Parent class:
\FastForward\Iterator\CountableIteratorIterator
Initializes the ConsecutiveGroupIterator.
public __construct(iterable $iterator, \Closure $callback): mixedParameters:
| 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. |
Retrieves the current chunk of elements.
public current(): array<int,mixed>Return Value:
the current chunk as an array
Retrieves the current group key.
public key(): int|nullReturn Value:
the current group key, or null if the iterator is not valid
Advances to the next chunk of elements.
public next(): voidChecks if the current chunk contains valid elements.
public valid(): boolReturn Value:
true if a chunk exists, false otherwise
Resets the iterator and prepares the first chunk.
public rewind(): voidCounts the number of elements in the iterable.
public count(): intIf 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