RangeIterator

Class
implements Iterator Countable

An iterator that behaves like PHP's `range()` function.

Description

It supports both ascending and descending sequences and allows iteration over floating-point or integer ranges.

Usage Example:

Tags
example

Description

Iterating Over an Integer Range

use FastForward\Iterator\RangeIterator;

$iterator = new RangeIterator(1, 10, 2);

foreach ($iterator as $key => $value) {
    echo "[$key] => $value\n";
}
// Output:
// [0] => 1
// [1] => 3
// [2] => 5
// [3] => 7
// [4] => 9
example

Description

Iterating Over a Floating-Point Range

use FastForward\Iterator\RangeIterator;

$iterator = new RangeIterator(0.5, 2.5, 0.5);

foreach ($iterator as $value) {
    echo $value . "\n";
}
// Output:
// 0.5
// 1.0
// 1.5
// 2.0
// 2.5
example

Description

Iterating Over a Floating-Point Range Including the Boundary

use FastForward\Iterator\RangeIterator;

$iterator = new RangeIterator(0, 5, 1.5, true);

foreach ($iterator as $value) {
    echo $value . "\n";
}
// Output:
// 0
// 1.5
// 3.0
// 4.5
// 5.0

Note: If the step is larger than the absolute difference between start and end, an InvalidArgumentException is thrown.

since
1.0.0

Table of Contents

Interfaces

Constants

 = 1.0E-12

Properties

 : float|int
 : float|int
 : int
 : float|int
 : float|int

Methods

__construct()

Initializes the RangeIterator.

 : mixed
count()

Counts the total number of steps in the range.

 : int
current()

Returns the current value in the range.

 : float|int
key()

Returns the current key (index).

 : int
next()

Moves to the next value in the range.

 : void
rewind()

Resets the iterator to the start of the range.

 : void
valid()

Checks if the current position is within the valid range.

 : bool
isAtBoundary()

Checks whether the given value is effectively equal to the range boundary.

 : bool
wouldOvershoot()

Checks whether the projected next value would exceed the range boundary.

 : bool
Constants

Constants

EPSILON

Private
private float EPSILON = 1.0E-12

Description

floating-point comparison tolerance

Properties

$boundaryYielded

Private
private bool $boundaryYielded = false

Description

indicates whether the iterator should force the boundary value when the next step would overshoot the range

$current

Private
private float|int $current

Description

the current value in the iteration

$includeBoundary

Private Read-only
private bool $includeBoundary = false

$key

Private
private int $key = 0

Description

the current key (index) in the iteration

$step

Private Read-only
private float|int $step

Description

the step size for each iteration

Methods

__construct()

Public

Initializes the RangeIterator.

public __construct( float|int  $start, float|int  $end[, float|int  $step = 1][, bool  $includeBoundary = false]) : mixed
Parameters
$start : float|int

Description

the starting value of the range

$end : float|int

Description

the ending value of the range

$step : float|int = 1

Description

the step size between values (must be positive)

$includeBoundary : bool = false

Description

whether the iterator should force the end value when the next step would overshoot it

Tags
throws
InvalidArgumentException

Description

if step is non-positive or greater than the range size

count()

Public

Counts the total number of steps in the range.

public count() : int
Return values
int

Description

the number of elements in the range

current()

Public

Returns the current value in the range.

public current() : float|int
Return values
float|int

Description

the current value

key()

Public

Returns the current key (index).

public key() : int
Return values
int

Description

the current index in the iteration

next()

Public

Moves to the next value in the range.

public next() : void

rewind()

Public

Resets the iterator to the start of the range.

public rewind() : void

valid()

Public

Checks if the current position is within the valid range.

public valid() : bool
Return values
bool

Description

true if the current value is within the valid range, false otherwise

isAtBoundary()

Private

Checks whether the given value is effectively equal to the range boundary.

private isAtBoundary( float|int  $value) : bool

Description

This method accounts for floating-point precision issues by using a tolerance value (EPSILON) to determine if the current value is close enough to the end value to be considered at the boundary.

Parameters
$value : float|int

Description

the value to check against the boundary

Return values
bool

Description

true if the value is effectively at the boundary, false otherwise

wouldOvershoot()

Private

Checks whether the projected next value would exceed the range boundary.

private wouldOvershoot( float|int  $projected) : bool

Description

This method is used to determine if the next step would overshoot the end value, which is important when the includeBoundary option is enabled to ensure thatthe boundary value is yielded when appropriate.

Parameters
$projected : float|int

Description

the next value after applying the step

Return values
bool

Description

true if the projected value would overshoot the end boundary, false otherwise