ChainIterableIterator

Class
implements Iterator Countable
Final: Yes

An iterator that chains multiple iterable sources together into a single unified iterator.

Description

This iterator SHALL accept any number of iterable values (arrays, Traversables, or Iterators) and iterate over them in order. When the current iterator is exhausted, it proceeds to the next.

The class MUST ensure all incoming values are wrapped as \Iterator instances, either natively or by converting Traversables or arrays using standard SPL iterators.

Example usage:

$it = new ChainIterableIterator([1, 2], new ArrayIterator([3, 4]));
foreach ($it as $value) {
    echo $value;
}
// Output: 1234
Tags
since
1.1.0

Table of Contents

Interfaces

Properties

 : array<string|int, Iterator>
 : int

Methods

__construct()

Constructs a ChainIterableIterator with one or more iterable sources.

 : mixed
count()

Counts the total number of elements across all chained iterators.

 : int
current()

Returns the current element from the active iterator.

 : mixed|null
 : int|null
next()

Moves the pointer of the active iterator forward.

 : void
rewind()

Rewinds all underlying iterators and resets the position.

 : void
valid()

Checks whether the current position is valid across chained iterators.

 : bool
Properties

$currentIndex

Private
private int $currentIndex = 0

Description

the index of the currently active iterator

$iterators

Private
private array<string|int, Iterator> $iterators

Description

a list of iterators chained in sequence

$position

Private
private int $position = 0

Description

the normalized sequential key across all chained iterators

Methods

__construct()

Public

Constructs a ChainIterableIterator with one or more iterable sources.

public __construct( iterable<string|int, mixed>  ...$iterables) : mixed

Description

Each iterable SHALL be normalized to a \Iterator instance using:

  • \ArrayIterator for arrays
  • \IteratorIterator for Traversable objects
  • Directly used if already an \Iterator
Parameters
$iterables : iterable<string|int, mixed>

Description

One or more iterable data sources to chain.

count()

Public

Counts the total number of elements across all chained iterators.

public count() : int

Description

This method iterates through each underlying iterator and sums their counts.

Return values
int

Description

the total count of elements in all chained iterators

current()

Public

Returns the current element from the active iterator.

public current() : mixed|null
Return values
mixed|null

Description

the current element or NULL if iteration is invalid

rewind()

Public

Rewinds all underlying iterators and resets the position.

public rewind() : void

Description

Each chained iterator SHALL be rewound to its beginning.

valid()

Public

Checks whether the current position is valid across chained iterators.

public valid() : bool

Description

Iteration continues until the current iterator is valid or all are exhausted.

Return values
bool

Description

TRUE if there are more elements to iterate; FALSE otherwise