Groups elements dynamically based on a user-defined condition.
Description
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:
Description
Grouping consecutive equal elements
use FastForward\Iterator\ConsecutiveGroupIterator;
use ArrayIterator;
$data = new ArrayIterator([1, 1, 2, 2, 2, 3, 4, 4, 5]);
$chunkedIterator = new ConsecutiveGroupIterator($data, fn($prev, $curr) => $prev === $curr);
foreach ($chunkedIterator as $chunk) {
print_r($chunk);
}
// Outputs:
// [1, 1]
// [2, 2, 2]
// [3]
// [4, 4]
// [5]
Note: The chunking behavior is defined by the callback function.
Properties
Methods
Initializes the ConsecutiveGroupIterator.
Counts the number of elements exposed by the inner iterator.
Retrieves the current chunk of elements.
Retrieves the current group key.
Advances to the next chunk of elements.
Resets the iterator and prepares the first chunk.
Checks if the current chunk contains valid elements.
Loads the next chunk of elements based on the callback condition.
Initializes the ConsecutiveGroupIterator.
public
__construct(
iterable<string|int, mixed>
$iterator, Closure
$callback) : mixed
Parameters
$iterator
:
iterable<string|int, mixed>
Description
the iterator containing values to be chunked
$callback
:
Closure
Description
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.
Counts the number of elements exposed by the inner iterator.
public
count() : int
Description
If the inner iterator implements 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 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 values
Description
the total number of elements available from the inner iterator
Retrieves the current chunk of elements.
public
current() : array<int, mixed>
Return values
Description
the current chunk as an array
Retrieves the current group key.
public
key() : int|null
Return values
Description
the current group key, or null if the iterator is not valid
Advances to the next chunk of elements.
public
next() : void
Resets the iterator and prepares the first chunk.
public
rewind() : void
Checks if the current chunk contains valid elements.
public
valid() : bool
Return values
Description
true if a chunk exists, false otherwise
Loads the next chunk of elements based on the callback condition.
private
loadChunk() : bool
Description
This method fills the buffer with consecutive elements that satisfy the callback condition.
Return values
Description
true if a chunk was loaded, false otherwise