SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Private Attributes
MultipleIterator Class Reference
Inheritance diagram for MultipleIterator:
Inheritance graph
[legend]
Collaboration diagram for MultipleIterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 __construct ($flags=self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)
 attachIterator (Iterator $iter, $inf=NULL)
 containsIterator (Iterator $iter)
 countIterators ()
 current ()
 detachIterator (Iterator $iter)
 getFlags ()
 key ()
 next ()
 rewind ()
 setFlags ($flags)
 valid ()

Public Attributes

const MIT_KEYS_ASSOC = 2
const MIT_KEYS_NUMERIC = 0
const MIT_NEED_ALL = 1
const MIT_NEED_ANY = 0

Private Attributes

 $flags
 $iterators

Detailed Description

Iterator that iterates over several iterators one after the other.

Author:
Johannes Schlueter
Marcus Boerger
Version:
1.0
Since:
PHP 5.3

Definition at line 19 of file multipleiterator.inc.


Constructor & Destructor Documentation

MultipleIterator::__construct ( flags = self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)

Construct a new empty MultipleIterator.

Parameters:
flagsMIT_* flags

Definition at line 42 of file multipleiterator.inc.

References $flags.

    {
        $this->iterators = new SplObjectStorage();
        $this->flags = $flags;
    }

Member Function Documentation

MultipleIterator::attachIterator ( Iterator iter,
inf = NULL 
)
Parameters:
$iternew Iterator to attach.
$infassociative info forIteraotr, must be NULL, integer or string
Exceptions:
IllegalValueExceptionif a inf is none of NULL, integer or string
IllegalValueExceptionif a inf is already an associated info

Definition at line 66 of file multipleiterator.inc.

    {

        if (!is_null($inf))
        {
            if (!is_int($inf) && !is_string($inf))
            {
                throw new IllegalValueException('Inf must be NULL, integer or string');
            }
            foreach($this->iterators as $iter)
            {
                if ($inf == $this->iterators->getInfo())
                {
                    throw new IllegalValueException('Key duplication error');
                }
            }
        }
        $this->iterators->attach($iter, $inf);
    }
MultipleIterator::containsIterator ( Iterator iter)
Parameters:
$iterIterator to check
Returns:
whether $iter is attached or not

Definition at line 95 of file multipleiterator.inc.

    {
        return $this->iterator->contains($iter);
    }
MultipleIterator::countIterators ( )
Returns:
number of attached Iterator instances.

Definition at line 101 of file multipleiterator.inc.

    {
        return $this->iterators->count();
    }
MultipleIterator::current ( )
Returns:
false if no sub Iterator is attached and an array of all registered Iterator instances current() result.
Exceptions:
RuntimeExceptionif mode MIT_NEED_ALL is set and at least one attached Iterator is not valid().
IllegalValueExceptionif a key is NULL and MIT_KEYS_ASSOC is set.

Implements Iterator.

Definition at line 157 of file multipleiterator.inc.

References $it.

    {
        if (!sizeof($this->iterators))
        {
            return false;
        }
        $retval = array();
        foreach($this->iterators as $iter)
        {
            if ($it->valid())
            {
                if ($this->flags & self::MIT_KEYS_ASSOC)
                {
                    $key = $this->iterators->getInfo();
                    if (is_null($key))
                    {
                        throw new IllegalValueException('Sub-Iterator is associated with NULL');
                    }
                    $retval[$key] = $iter->current();
                }
                else
                {
                    $retval[] = $iter->current();
                }
            }
            else if ($this->flags & self::MIT_NEED_ALL)
            {
                throw new RuntimeException('Called current() with non valid sub iterator');
            }
            else
            {
                $retval[] = NULL;
            }
        }
        return $retval;
    }
MultipleIterator::detachIterator ( Iterator iter)
Parameters:
$iterattached Iterator that should be detached.

Definition at line 87 of file multipleiterator.inc.

    {
        $this->iterators->detach($iter);
    }
MultipleIterator::getFlags ( )
Returns:
current flags MIT_*

Definition at line 49 of file multipleiterator.inc.

    {
        return $this->flags;
    }
MultipleIterator::key ( )
Returns:
false if no sub Iterator is attached and an array of all registered Iterator instances key() result.
Exceptions:
LogicExceptionif mode MIT_NEED_ALL is set and at least one attached Iterator is not valid().

Implements Iterator.

Definition at line 199 of file multipleiterator.inc.

References $it.

    {
        if (!sizeof($this->iterators))
        {
            return false;
        }
        $retval = array();
        foreach($this->iterators as $iter)
        {
            if ($it->valid())
            {
                $retval[] = $iter->key();
            }
            else if ($this->flags & self::MIT_NEED_ALL)
            {
                throw new LogicException('Called key() with non valid sub iterator');
            }
            else
            {
                $retval[] = NULL;
            }
        }
        return $retval;
    }
MultipleIterator::next ( )

Move all attached Iterator instances forward.

That is invoke their next() method regardless of their state.

Implements Iterator.

Definition at line 143 of file multipleiterator.inc.

    {
        foreach($this->iterators as $iter)
        {
            $iter->next();
        }
    }
MultipleIterator::rewind ( )

Rewind all attached Iterator instances.

Implements Iterator.

Definition at line 107 of file multipleiterator.inc.

    {
        foreach($this->iterators as $iter)
        {
            $iter->rewind();
        }
    }
MultipleIterator::setFlags ( flags)
Parameters:
$flagsnew flags.

Definition at line 55 of file multipleiterator.inc.

References $flags.

    {
        $this->flags = $flags;
    }
MultipleIterator::valid ( )
Returns:
whether all or one sub iterator is valid depending on flags. In mode MIT_NEED_ALL we expect all sub iterators to be valid and return flase on the first non valid one. If that flag is not set we return true on the first valid sub iterator found. If no Iterator is attached, we always return false.

Implements Iterator.

Definition at line 122 of file multipleiterator.inc.

    {
        if (!sizeof($this->iterators)) {
            return false;
        }
        // The following code is an optimized version that executes as few
        // valid() calls as necessary and that only checks the flags once.
        $expect = $this->flags & self::MIT_NEED_ALL ? true : false;
        foreach($this->iterators as $iter)
        {
            if ($expect != $iter->valid())
            {
                return !$expect;
            }
        }
        return $expect;
    }

Member Data Documentation

MultipleIterator::$flags [private]

Flags: const MIT_*.

Definition at line 25 of file multipleiterator.inc.

Referenced by __construct(), and setFlags().

MultipleIterator::$iterators [private]

Inner Iterators.

Definition at line 22 of file multipleiterator.inc.

keys are created from sub iterators associated infromation

Definition at line 37 of file multipleiterator.inc.

keys are created from sub iterators position

Definition at line 34 of file multipleiterator.inc.

require all sub iterators to be valid in iteration

Definition at line 31 of file multipleiterator.inc.

do not require all sub iterators to be valid in iteration

Definition at line 28 of file multipleiterator.inc.


The documentation for this class was generated from the following file: