2004-04-25 13:06:15 +00:00
|
|
|
<?php
|
|
|
|
|
2004-05-10 17:26:03 +00:00
|
|
|
/** @file appenditerator.inc
|
2004-11-01 00:26:59 +00:00
|
|
|
* @ingroup SPL
|
2004-05-10 17:26:03 +00:00
|
|
|
* @brief class AppendIterator
|
|
|
|
* @author Marcus Boerger
|
|
|
|
* @date 2003 - 2004
|
|
|
|
*
|
|
|
|
* SPL - Standard PHP Library
|
|
|
|
*/
|
|
|
|
|
2004-11-01 00:26:59 +00:00
|
|
|
/** @ingroup SPL
|
2004-04-25 13:06:15 +00:00
|
|
|
* @brief Iterator that iterates over several iterators one after the other
|
|
|
|
* @author Marcus Boerger
|
|
|
|
* @version 1.0
|
|
|
|
*/
|
2004-10-08 21:12:15 +00:00
|
|
|
class AppendIterator implements OuterIterator
|
2004-04-25 13:06:15 +00:00
|
|
|
{
|
2004-05-08 12:35:21 +00:00
|
|
|
/** @internal array of inner iterators */
|
|
|
|
private $iterators;
|
2004-04-25 13:06:15 +00:00
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** Construct an empty AppendIterator
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->iterators = new ArrayIterator();
|
|
|
|
}
|
2004-05-08 12:35:21 +00:00
|
|
|
|
|
|
|
/** Append an Iterator
|
|
|
|
* @param $it Iterator to append
|
2004-11-01 00:26:59 +00:00
|
|
|
*
|
|
|
|
* If the current state is invalid but the appended iterator is valid
|
|
|
|
* the the AppendIterator itself becomes valid. However there will be no
|
|
|
|
* call to $it->rewind(). Also if the current state is invalid the inner
|
|
|
|
* ArrayIterator will be rewound und forwarded to the appended element.
|
2004-05-08 12:35:21 +00:00
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function append(Iterator $it)
|
|
|
|
{
|
|
|
|
$this->iterators->append($it);
|
|
|
|
}
|
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** @return the current inner Iterator
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function getInnerIterator()
|
|
|
|
{
|
|
|
|
return $this->iterators->current();
|
|
|
|
}
|
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** Rewind to the first element of the first inner Iterator.
|
|
|
|
* @return void
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function rewind()
|
|
|
|
{
|
|
|
|
$this->iterators->rewind();
|
2004-04-26 22:01:12 +00:00
|
|
|
if ($this->iterators->valid())
|
|
|
|
{
|
2004-04-28 21:52:51 +00:00
|
|
|
$this->getInnerIterator()->rewind();
|
2004-04-26 22:01:12 +00:00
|
|
|
}
|
2004-04-25 13:06:15 +00:00
|
|
|
}
|
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** @return whether the current element is valid
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function valid()
|
|
|
|
{
|
|
|
|
return $this->iterators->valid() && $this->getInnerIterator()->valid();
|
|
|
|
}
|
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** @return the current value if it is valid or \c NULL
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function current()
|
|
|
|
{
|
2004-04-26 22:01:12 +00:00
|
|
|
/* Using $this->valid() would be exactly the same; it would omit
|
|
|
|
* the access to a non valid element in the inner iterator. Since
|
|
|
|
* the user didn't respect the valid() return value false this
|
|
|
|
* must be intended hence we go on. */
|
|
|
|
return $this->iterators->valid() ? $this->getInnerIterator()->current() : NULL;
|
2004-04-25 13:06:15 +00:00
|
|
|
}
|
|
|
|
|
2004-05-08 12:35:21 +00:00
|
|
|
/** @return the current key if it is valid or \c NULL
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function key()
|
|
|
|
{
|
2004-04-26 22:01:12 +00:00
|
|
|
return $this->iterators->valid() ? $this->getInnerIterator()->key() : NULL;
|
2004-04-25 13:06:15 +00:00
|
|
|
}
|
2004-05-08 12:35:21 +00:00
|
|
|
|
|
|
|
/** Move to the next element. If this means to another Iterator that
|
|
|
|
* rewind that Iterator.
|
|
|
|
* @return void
|
|
|
|
*/
|
2004-04-25 13:06:15 +00:00
|
|
|
function next()
|
|
|
|
{
|
2004-04-26 22:01:12 +00:00
|
|
|
if (!$this->iterators->valid())
|
|
|
|
{
|
|
|
|
return; /* done all */
|
|
|
|
}
|
|
|
|
$this->getInnerIterator()->next();
|
|
|
|
if ($this->getInnerIterator()->valid())
|
|
|
|
{
|
|
|
|
return; /* found valid element in current inner iterator */
|
|
|
|
}
|
|
|
|
$this->iterators->next();
|
|
|
|
while ($this->iterators->valid())
|
|
|
|
{
|
|
|
|
$this->getInnerIterator()->rewind();
|
|
|
|
if ($this->getInnerIterator()->valid())
|
|
|
|
{
|
|
|
|
return; /* found element as first elemet in another iterator */
|
2004-04-25 13:06:15 +00:00
|
|
|
}
|
|
|
|
$this->iterators->next();
|
|
|
|
}
|
|
|
|
}
|
2004-05-08 12:35:21 +00:00
|
|
|
|
|
|
|
/** Aggregates the inner iterator
|
|
|
|
*/
|
|
|
|
function __call($func, $params)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array($this->getInnerIterator(), $func), $params);
|
|
|
|
}
|
2004-04-25 13:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|