-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 exposed by the inner iterator.
public count(): intIf 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