RangeIterator
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:
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
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
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.
Interfaces
Constants
Properties
Methods
Initializes the RangeIterator.
Counts the total number of steps in the range.
Returns the current value in the range.
Returns the current key (index).
Moves to the next value in the range.
Resets the iterator to the start of the range.
Checks if the current position is within the valid range.
Checks whether the given value is effectively equal to the range boundary.
Checks whether the projected next value would exceed the range boundary.
private
bool
$boundaryYielded
=
false
Description
indicates whether the iterator should force the boundary value when the next step would overshoot the range
private
float|int
$current
Description
the current value in the iteration
private
float|int
$end
private
bool
$includeBoundary
=
false
private
int
$key
=
0
Description
the current key (index) in the iteration
private
float|int
$start
private
float|int
$step
Description
the step size for each iteration
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
Description
if step is non-positive or greater than the range size
Counts the total number of steps in the range.
public
count() : int
Return values
Description
the number of elements in the range
Returns the current value in the range.
public
current() : float|int
Return values
Description
the current value
Returns the current key (index).
public
key() : int
Return values
Description
the current index in the iteration
Moves to the next value in the range.
public
next() : void
Resets the iterator to the start of the range.
public
rewind() : void
Checks if the current position is within the valid range.
public
valid() : bool
Return values
Description
true if the current value is within the valid range, false otherwise
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
Description
true if the value is effectively at the boundary, false otherwise
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
Description
true if the projected value would overshoot the end boundary, false otherwise