ConsecutiveGroupIterator

Class

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:

Tags
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.

since
1.0.0

Table of Contents

Properties

 : array<int, mixed>
 : Closure
 : int

Methods

__construct()

Initializes the ConsecutiveGroupIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
current()

Retrieves the current chunk of elements.

 : array<int, mixed>
key()

Retrieves the current group key.

 : int|null
next()

Advances to the next chunk of elements.

 : void
rewind()

Resets the iterator and prepares the first chunk.

 : void
valid()

Checks if the current chunk contains valid elements.

 : bool
loadChunk()

Loads the next chunk of elements based on the callback condition.

 : bool
Properties

$buffer

Private
private array<int, mixed> $buffer = []

Description

the buffer storing the current chunk of elements

Methods

__construct()

Public

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.

count()

Public

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
int

Description

the total number of elements available from the inner iterator

current()

Public

Retrieves the current chunk of elements.

public current() : array<int, mixed>
Return values
array<int, mixed>

Description

the current chunk as an array

key()

Public

Retrieves the current group key.

public key() : int|null
Return values
int|null

Description

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

valid()

Public

Checks if the current chunk contains valid elements.

public valid() : bool
Return values
bool

Description

true if a chunk exists, false otherwise

loadChunk()

Private

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
bool

Description

true if a chunk was loaded, false otherwise