RepeatableIteratorIterator

Class
extends LimitIterator
implements Countable

An iterator that enables repeated iteration over a finite subset of an infinite iterator.

Description

This iterator wraps an Iterator and applies a LimitIterator over an InfiniteIterator, allowing a limited number of repeated iterations without modifying the underlying iterator.

It also implements Countable to return the number of elements available in the limited iteration.

Usage Example:

Tags
example

Description

Limiting an Infinite Iterator

use FastForward\Iterator\RepeatableIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator([1, 2, 3, 4, 5]);
$repeatableIterator = new RepeatableIteratorIterator($data, 3);

echo count($repeatableIterator); // Outputs: 3

foreach ($repeatableIterator as $value) {
    echo $value . ' '; // Outputs: 1 2 3
}

Note: The iterator does not consume values permanently, as it is backed by an InfiniteIterator and LimitIterator.

since
1.0.0

Table of Contents

Interfaces

Properties

 : int

Methods

__construct()

Initializes the RepeatableIteratorIterator.

 : mixed
count()

Returns the number of elements that can be iterated per cycle.

 : int
Properties
Methods

__construct()

Public

Initializes the RepeatableIteratorIterator.

public __construct( iterable<string|int, mixed>  $iterator, int  $limit[, int  $offset = 0]) : mixed
Parameters
$iterator : iterable<string|int, mixed>

Description

the iterator to be wrapped in an infinite loop

$limit : int

Description

the maximum number of elements to iterate over per cycle

$offset : int = 0

Description

the starting offset within the iterator

count()

Public

Returns the number of elements that can be iterated per cycle.

public count() : int
Return values
int

Description

the count of items in the limited iteration