2003-11-09 14:05:36 +00:00
|
|
|
<?php
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** @file filteriterator.inc
|
2004-10-31 19:05:37 +00:00
|
|
|
* @ingroup SPL
|
2004-10-29 20:58:58 +00:00
|
|
|
* @brief class FilterIterator
|
|
|
|
* @author Marcus Boerger
|
|
|
|
* @date 2003 - 2004
|
|
|
|
*
|
|
|
|
* SPL - Standard PHP Library
|
|
|
|
*/
|
|
|
|
|
2003-11-09 14:05:36 +00:00
|
|
|
/**
|
|
|
|
* @brief Regular expression filter for string iterators
|
|
|
|
* @author Marcus Boerger
|
2004-10-29 20:58:58 +00:00
|
|
|
* @version 1.1
|
2003-11-09 14:05:36 +00:00
|
|
|
*
|
2003-11-30 16:31:35 +00:00
|
|
|
* Instances of this class act as a filter around iterators. In other words
|
|
|
|
* you can put an iterator into the constructor and the instance will only
|
|
|
|
* return selected (accepted) elements.
|
2004-11-01 18:11:39 +00:00
|
|
|
*
|
|
|
|
* The only thing that needs to be done to make this work is implementing
|
|
|
|
* method accept(). Typically this invloves reading the current element or
|
|
|
|
* key of the inner Iterator and checking whether it is acceptable.
|
2003-11-09 14:05:36 +00:00
|
|
|
*/
|
2004-10-29 20:12:57 +00:00
|
|
|
abstract class FilterIterator implements OuterIterator
|
2003-11-09 14:05:36 +00:00
|
|
|
{
|
2005-02-08 19:00:19 +00:00
|
|
|
private $it;
|
2003-11-09 14:05:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a filter around an iterator whose elemnts are strings.
|
|
|
|
* If the given iterator is of type spl_sequence then its rewind()
|
|
|
|
* method is called.
|
|
|
|
*
|
|
|
|
* @param it Object that implements at least spl_forward
|
|
|
|
*/
|
|
|
|
function __construct(Iterator $it) {
|
|
|
|
$this->it = $it;
|
|
|
|
}
|
|
|
|
|
2003-11-30 16:31:35 +00:00
|
|
|
/**
|
|
|
|
* Rewind the inner iterator.
|
|
|
|
*/
|
2003-11-09 14:05:36 +00:00
|
|
|
function rewind() {
|
|
|
|
$this->it->rewind();
|
|
|
|
$this->fetch();
|
|
|
|
}
|
|
|
|
|
2003-11-30 16:31:35 +00:00
|
|
|
/**
|
|
|
|
* Accept function to decide whether an element of the inner iterator
|
|
|
|
* should be accessible through the Filteriterator.
|
|
|
|
*
|
|
|
|
* @return whether or not to expose the current element of the inner
|
|
|
|
* iterator.
|
|
|
|
*/
|
2003-11-09 14:05:36 +00:00
|
|
|
abstract function accept();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch next element and store it.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function fetch() {
|
2004-03-08 17:33:31 +00:00
|
|
|
while ($this->it->valid()) {
|
2003-11-09 14:05:36 +00:00
|
|
|
if ($this->accept()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->it->next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move to next element
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function next() {
|
|
|
|
$this->it->next();
|
|
|
|
$this->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether more elements are available
|
|
|
|
*/
|
2004-03-08 17:33:31 +00:00
|
|
|
function valid() {
|
|
|
|
return $this->it->valid();
|
2003-11-09 14:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The current key
|
|
|
|
*/
|
|
|
|
function key() {
|
|
|
|
return $this->it->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The current value
|
|
|
|
*/
|
|
|
|
function current() {
|
|
|
|
return $this->it->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hidden __clone
|
|
|
|
*/
|
|
|
|
protected function __clone() {
|
|
|
|
// disallow clone
|
|
|
|
}
|
2004-10-29 20:12:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The inner iterator
|
|
|
|
*/
|
|
|
|
function getInnerIterator()
|
|
|
|
{
|
|
|
|
return $this->it;
|
|
|
|
}
|
2004-10-31 19:05:37 +00:00
|
|
|
|
|
|
|
/** Aggregate the inner iterator
|
|
|
|
*
|
|
|
|
* @param func Name of method to invoke
|
|
|
|
* @param params Array of parameters to pass to method
|
|
|
|
*/
|
|
|
|
function __call($func, $params)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array($this->it, $func), $params);
|
|
|
|
}
|
2003-11-09 14:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|