TrimIteratorIterator

Class

An iterator that trims each value within a `iterable`.

Description

This class extends ClosureIteratorIterator and applies trim() to each element, removing leading and trailing characters based on the specified character mask.

Usage Example:

Tags
example

Description

Basic Usage

use FastForward\Iterator\TrimIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator(["  hello  ", "\nworld\n", "\tPHP\t"]);
$trimIterator = new TrimIteratorIterator($data);

foreach ($trimIterator as $value) {
    echo $value . ' | '; // Outputs: "hello | world | PHP | "
}
example

Description

Custom Trim Characters

use FastForward\Iterator\TrimIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator(["--Hello--", "**World**", "!!PHP!!"]);
$trimIterator = new TrimIteratorIterator($data, "-*!");

foreach ($trimIterator as $value) {
    echo $value . ' | '; // Outputs: "Hello | World | PHP | "
}

Note: If $characters is null, the default trim characters (" \n\r\t\v\x00") will be used.

since
1.0.0

Table of Contents

Constants

 = " \n\r\t\v\x00"

Methods

__construct()

Initializes the TrimIteratorIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
current()

Returns the current transformed element.

 : mixed
Constants

Constants

DEFAULT_CHARACTERS

Private
private string DEFAULT_CHARACTERS = " \n\r\t\v\x00"

Description

the default characters to trim

Methods

__construct()

Public

Initializes the TrimIteratorIterator.

public __construct( iterable<string|int, mixed>  $iterator[, string|null  $characters = self::DEFAULT_CHARACTERS]) : mixed
Parameters
$iterator : iterable<string|int, mixed>

Description

the iterator containing values to be trimmed

$characters : string|null = self::DEFAULT_CHARACTERS

Description

A string defining the characters to be trimmed. Defaults to standard whitespace characters.

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

Returns the current transformed element.

public current() : mixed

Description

The closure is applied to the original current element of the iterator.

Return values

Description

the transformed element