php-src/ext/spl/examples/cachingiterator.inc

71 lines
1.0 KiB
PHP
Raw Normal View History

2003-11-18 22:18:38 +00:00
<?php
class CachingIterator
{
protected $it;
protected $current;
protected $key;
protected $more;
protected $strvalue;
2003-11-22 20:51:15 +00:00
function __construct(Iterator $it)
{
2003-11-18 22:18:38 +00:00
$this->it = $it;
}
2003-11-22 20:51:15 +00:00
function rewind()
{
2003-11-18 22:18:38 +00:00
$this->it->rewind();
$this->next();
}
2003-11-22 20:51:15 +00:00
function next()
{
2003-11-18 22:18:38 +00:00
if ($this->more = $this->it->hasMore()) {
$this->current = $this->it->current();
$this->key = $this->it->key();
2003-12-04 20:56:32 +00:00
if (is_object($this->current)) {
$this->strvalue = $this->current->__toString();
} else {
$this->strvalue = (string)$this->current;
}
2003-11-18 22:18:38 +00:00
} else {
$this->current = NULL;
$this->key = NULL;
$this->strvalue = '';
}
$this->it->next();
}
2003-11-22 20:51:15 +00:00
function hasMore()
{
2003-11-18 22:18:38 +00:00
return $this->more;
}
2003-11-22 20:51:15 +00:00
function hasNext()
{
2003-11-18 22:18:38 +00:00
return $this->it->hasMore();
}
2003-11-22 20:51:15 +00:00
function current()
{
2003-11-18 22:18:38 +00:00
return $this->current;
}
2003-11-22 20:51:15 +00:00
function key()
{
2003-11-18 22:18:38 +00:00
return $this->key;
}
2003-11-22 20:51:15 +00:00
function __call($func, $params)
{
2003-11-18 22:18:38 +00:00
return call_user_func_array(array($this->it, $func), $params);
}
2003-11-22 20:51:15 +00:00
function __toString()
{
2003-11-18 22:18:38 +00:00
return $this->strvalue;
}
}
?>