PHP 5.6.18 is available

La clase IteratorIterator

(PHP 5 >= 5.1.0, PHP 7)

Introducción

Esta envoltura de iteradores permite la conversión de todo aquello que es Traversable a un iterador. Es importante comprender que la mayoría de las clases que no implementan iteradores, lo más probable es que no permitan todas las características del iterador. Si es así, deberían proporcionarse técnicas para evitar un uso indebido, de lo contrario se pueden esperar excepciones o errores fatales.

Sinopsis de la Clase

IteratorIterator implements OuterIterator {
/* Métodos */
public __construct ( Traversable $iterator )
public mixed current ( void )
public Traversable getInnerIterator ( void )
public scalar key ( void )
public void next ( void )
public void rewind ( void )
public bool valid ( void )
}

Notas

Nota:

Esta clase permite el acceso a métodos del iterador interno mediante el método mágico __call.

Tabla de contenidos

add a note add a note

User Contributed Notes 2 notes

up
6
sven at rtbg dot de
9 months ago
This iterator basically is only a wrapper around another iterator. It does nothing fancy, it simply forwards any calls of rewind(), next(), valid(), current() and key() to the inner iterator. This inner iterator can be fetched with getInnerIterator().

One special case: When passing an IteratorAggregate object, the getIterator() method of that object will be called and THAT iterator will be iterated over, and this will also be returned when calling getInnerIterator().

This class can be extended, so it's an ideal building block for your own classes that only want to modify one or two of the iterator methods, but not all.

Want to trim the strings returned by the current() method?

<?php

class TrimIterator extends IteratorIterator
{
    public function
current() {
        return
trim(parent::current());
    }
}

$innerIterator = new ArrayIterator(array('normal', ' trimmable '));

$trim = new TrimIterator($innerIterator);

foreach (
$trim as $key => $value) {
    echo
"Key:\n";
   
var_dump($key);
    echo
"Value:\n";
   
var_dump($value);
    echo
"---next---";
}
?>

Output:

Key:
int(0)
Value:
string(6) "normal"
---next---Key:
int(1)
Value:
string(9) "trimmable"
---next---
up
1
wallacemaxters at gmail dot lcom
22 days ago
Another example of the efficiency for IteratorIterator is a small class for enumerate for iterations of an interator implementation.

Example:

<?php

class Enumerator extends IteratorIterator
{   
   
/**
    * Initial value for enumerator
    * @param int 
    */
   
protected $start = 0;

   
/**
    * @param int
    */
   
protected $key = 0;

   
/**
    * @param Traversable $iterator
    * @param scalar $start
    */
   
public function __construct(Traversable $iterator, $start = 0)
    {
       
parent::__construct($iterator);

       
$this->start = $start;

       
$this->key = $this->start;
    }

    public function
key()var_dump
   
{
        return
$this->key;
    }

    public function
next()
    {
        ++
$this->key;

       
parent::next();
    }

    public function
rewind()
    {
       
$this->key = $this->start;

       
parent::rewind();
    }

}
?>

This produces:

<?php

$enumerator
= new Enumerator(
       new
ArrayIterator(['php', 'java', 'python']); 7000
);

print_r(iterator_to_array($enumerator));

/*
*    array(3) {
           7000 => 'php',
           7001 => 'java',
           7002 => 'python'
     }
*/

?>
To Top