diff options
author | Marcus Boerger <helly@php.net> | 2003-12-06 19:03:17 +0000 |
---|---|---|
committer | Marcus Boerger <helly@php.net> | 2003-12-06 19:03:17 +0000 |
commit | 8e188f9d038de465eed9ba2bac5881939860ad56 (patch) | |
tree | 9d6c5e66d81b1b11bee0303c270f5758a299c99c /ext | |
parent | 8a8deee8a01956bc276f91a8fd5f9561691891a9 (diff) | |
download | php-git-8e188f9d038de465eed9ba2bac5881939860ad56.tar.gz |
Update examples
Diffstat (limited to 'ext')
-rwxr-xr-x | ext/spl/examples/autoload.inc | 4 | ||||
-rw-r--r-- | ext/spl/examples/cachingiterator.inc | 23 | ||||
-rwxr-xr-x | ext/spl/examples/directorytree.php | 1 | ||||
-rwxr-xr-x | ext/spl/examples/limititerator.inc | 44 | ||||
-rwxr-xr-x | ext/spl/examples/seekableiterator.inc | 15 |
5 files changed, 64 insertions, 23 deletions
diff --git a/ext/spl/examples/autoload.inc b/ext/spl/examples/autoload.inc index 632ab8e4e8..34072f8e4a 100755 --- a/ext/spl/examples/autoload.inc +++ b/ext/spl/examples/autoload.inc @@ -1,7 +1,7 @@ <?php -function __autoload($file) { - require_once(dirname($_SERVER['PATH_TRANSLATED']).'/'.strtolower($file).'.inc'); +function __autoload($classname) { + require_once(dirname($_SERVER['PATH_TRANSLATED']).'/'.strtolower($classname).'.inc'); } ?>
\ No newline at end of file diff --git a/ext/spl/examples/cachingiterator.inc b/ext/spl/examples/cachingiterator.inc index c54f650e7e..a9cc075b70 100644 --- a/ext/spl/examples/cachingiterator.inc +++ b/ext/spl/examples/cachingiterator.inc @@ -6,11 +6,13 @@ class CachingIterator protected $current; protected $key; protected $more; - protected $strvalue; + protected $strValue; + protected $getStrVal; - function __construct(Iterator $it) + function __construct(Iterator $it, $getStrVal = true) { $this->it = $it; + $this->getStrVal = (boolean)$getStrVal; } function rewind() @@ -24,15 +26,17 @@ class CachingIterator if ($this->more = $this->it->hasMore()) { $this->current = $this->it->current(); $this->key = $this->it->key(); - if (is_object($this->current)) { - $this->strvalue = $this->current->__toString(); - } else { - $this->strvalue = (string)$this->current; + if ($this->getStrVal) { + if (is_object($this->current)) { + $this->strValue = $this->current->__toString(); + } else { + $this->strValue = (string)$this->current; + } } } else { $this->current = NULL; $this->key = NULL; - $this->strvalue = ''; + $this->strValue = ''; } $this->it->next(); } @@ -64,7 +68,10 @@ class CachingIterator function __toString() { - return $this->strvalue; + if (!$this->getStrVal) { + throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)'); + } + return $this->strValue; } } diff --git a/ext/spl/examples/directorytree.php b/ext/spl/examples/directorytree.php index 31af2495a7..ff644f6602 100755 --- a/ext/spl/examples/directorytree.php +++ b/ext/spl/examples/directorytree.php @@ -25,6 +25,7 @@ EOF; $length = $argc > 3 ? $argv[3] : NULL; foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as $file) { +//foreach(new DirectoryTreeIterator($argv[1]) as $file) { echo $file ."\n"; } diff --git a/ext/spl/examples/limititerator.inc b/ext/spl/examples/limititerator.inc index 1b0e927a2e..e471f5d0e4 100755 --- a/ext/spl/examples/limititerator.inc +++ b/ext/spl/examples/limititerator.inc @@ -5,33 +5,49 @@ class LimitIterator implements Iterator protected $it; protected $offset; protected $count; - protected $index; + private $pos; - // negative offset is respected // count === NULL means all - function __construct(Iterator $it, $offset = 0, $count = NULL) + function __construct(Iterator $it, $offset = 0, $count = -1) { + if ($offset < 0) { + throw new exception('Parameter offset must be > 0'); + } + if ($count < 0 && $count != -1) { + throw new exception('Parameter count must either be -1 or a value greater than or equal to 0'); + } $this->it = $it; $this->offset = $offset; $this->count = $count; - $this->index = 0; + $this->pos = 0; } - function rewind() - { - $this->it->rewind(); - $this->index = 0; + function seek($position) { + if ($position < $this->offset) { + throw new exception('Cannot seek to '.$position.' which is below offset '.$this->offset); + } + if ($position > $this->offset + $this->count && $this->count != -1) { + throw new exception('Cannot seek to '.$position.' which is behind offset '.$this->offset.' plus count '.$this->count); + } if ($this->it instanceof SeekableIterator) { - $this->index = $this->it->seek($this->offset); + $this->it->seek($position); + $this->pos = $position; } else { - while($this->index < $this->offset && $this->it->hasMore()) { + while($this->pos < $position && $this->it->hasMore()) { $this->next(); } } } + + function rewind() + { + $this->it->rewind(); + $this->pos = 0; + $this->seek($this->offset); + } function hasMore() { - return (is_null($this->count) || $this->index < $this->offset + $this->count) + return ($this->count == -1 || $this->pos < $this->offset + $this->count) && $this->it->hasMore(); } @@ -45,7 +61,11 @@ class LimitIterator implements Iterator function next() { $this->it->next(); - $this->index++; + $this->pos++; + } + + function getPosition() { + return $this->pos; } } diff --git a/ext/spl/examples/seekableiterator.inc b/ext/spl/examples/seekableiterator.inc index d7381fc3f8..7e47009260 100755 --- a/ext/spl/examples/seekableiterator.inc +++ b/ext/spl/examples/seekableiterator.inc @@ -1,7 +1,21 @@ <?php +/** \brief seekable iterator + * + * Turns a normal iterator ino a seekable iterator. When there is a way + * to seek on an iterator LimitIterator can use this to efficiently rewind + * to offset. + */ interface SeekableIterator implements Iterator { + /** Seek to an absolute position + * + * \param $index position to seek to + * \return void + * + * \note The method should throw an exception if it is not possible to + * seek to the given position. + */ function seek($index); /* $this->rewind(); $position = 0; @@ -9,7 +23,6 @@ interface SeekableIterator implements Iterator $this->next(); $position++; } - return $position; }*/ } |