CachingIterator Class Reference

Cached iteration over another Iterator. More...

Inheritance diagram for CachingIterator:

Inheritance graph
{RecursiveCachingIterator\n|- $getChildren\l- $hasChildren\l|+ __construct()\l+ getChildren()\l+ hasChildren()\l+ rewind()\l}{OuterIterator\n||+ getInnerIterator()\l}{Iterator\n||+ current()\l+ key()\l+ next()\l+ rewind()\l+ valid()\l}{Traversable\n||}{CachingRecursiveIterator\n||}
[legend]
Collaboration diagram for CachingIterator:

Collaboration graph
{OuterIterator\n||+ getInnerIterator()\l}{Iterator\n||+ current()\l+ key()\l+ next()\l+ rewind()\l+ valid()\l}{Traversable\n||}
[legend]
List of all members.

Public Member Functions

 __call ($func, $params)
 __construct (Iterator $it, $flags=self::CALL_TOSTRING)
 __toString ()
 current ()
 getInnerIterator ()
 hasNext ()
 key ()
 next ()
 rewind ()
 valid ()

Public Attributes

const CALL_TOSTRING = 0x00000001
const CATCH_GET_CHILD = 0x00000002
const TOSTRING_USE_CURRENT = 0x00000020
const TOSTRING_USE_KEY = 0x00000010

Private Attributes

 $current
 $it
 $key
 $strValue
 $valid

Detailed Description

Cached iteration over another Iterator.

Author:
Marcus Boerger
Version:
1.2
Since:
PHP 5.0
This iterator wrapper does a one ahead iteration. This way it knows whether the inner iterator has one more element.

Note:
If you want to convert the elements into strings and the inner Iterator is an internal Iterator then you need to provide the flag CALL_TOSTRING to do the conversion when the actual element is being fetched. Otherwise the conversion would happen with the already changed iterator. If you do not need this then it you should omit this flag because it costs unneccessary work and time.

Definition at line 28 of file cachingiterator.inc.


Constructor & Destructor Documentation

CachingIterator::__construct ( Iterator it,
flags = self::CALL_TOSTRING 
)

Construct from another iterator.

Parameters:
it Iterator to cache
flags Bitmask:
  • CALL_TOSTRING (whether to call __toString() for every element)

Definition at line 47 of file cachingiterator.inc.

References $it, and next().

Referenced by __toString().

00048     {
00049         if ((($flags & self::CALL_TOSTRING) && ($flags & (self::TOSTRING_USE_KEY|self::TOSTRING_USE_CURRENT)))
00050         || ((flags & (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)) == (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)))
00051         {
00052             throw new InvalidArgumentException('Flags must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, CIT_TOSTRING_USE_CURRENT');
00053         }
00054         $this->it = $it;
00055         $this->flags = $flags & (0x0000FFFF);
00056         $this->next();
00057     }

Here is the call graph for this function:

CachingIterator::nextCachingIterator::currentCachingIterator::keyCachingIterator::valid


Member Function Documentation

CachingIterator::__call ( func,
params 
)

Aggregate the inner iterator.

Parameters:
func Name of method to invoke
params Array of parameters to pass to method

Definition at line 122 of file cachingiterator.inc.

00123     {
00124         return call_user_func_array(array($this->it, $func), $params);
00125     }

CachingIterator::__toString (  ) 

Returns:
the string represenatation that was generated for the current element

Exceptions:
exception when CALL_TOSTRING was not specified in constructor

Definition at line 131 of file cachingiterator.inc.

References __construct(), current(), and key().

00132     {
00133         if ($this->flags & self::TOSTRING_USE_KEY)
00134         {
00135             return $this->key;
00136         }
00137         else if ($this->flags & self::TOSTRING_USE_CURRENT)
00138         {
00139             return $this->current;
00140         }
00141         if (!$this->flags & self::CALL_TOSTRING)
00142         {
00143             throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
00144         }
00145         return $this->strValue;
00146     }

Here is the call graph for this function:

CachingIterator::__constructCachingIterator::currentCachingIterator::keyCachingIterator::nextCachingIterator::valid

CachingIterator::current (  ) 

Returns:
the current element

Implements Iterator.

Definition at line 105 of file cachingiterator.inc.

Referenced by __toString(), and next().

00106     {
00107         return $this->current;
00108     }

CachingIterator::getInnerIterator (  ) 

Returns:
The inner iterator

Implements OuterIterator.

Definition at line 151 of file cachingiterator.inc.

00152     {
00153         return $this->it;
00154     }

CachingIterator::hasNext (  ) 

Returns:
whether there is one more element

Definition at line 98 of file cachingiterator.inc.

00099     {
00100         return $this->it->valid();
00101     }

CachingIterator::key (  ) 

Returns:
the current key

Implements Iterator.

Definition at line 112 of file cachingiterator.inc.

Referenced by __toString(), and next().

00113     {
00114         return $this->key;
00115     }

CachingIterator::next (  ) 

Forward to the next element.

Implements Iterator.

Definition at line 69 of file cachingiterator.inc.

References current(), key(), and valid().

Referenced by __construct(), and rewind().

00070     {
00071         if ($this->valid = $this->it->valid()) {
00072             $this->current = $this->it->current();
00073             $this->key = $this->it->key();
00074             if ($this->flags & self::CALL_TOSTRING) {
00075                 if (is_object($this->current)) {
00076                     $this->strValue = $this->current->__toString();
00077                 } else {
00078                     $this->strValue = (string)$this->current;
00079                 }
00080             }
00081         } else {
00082             $this->current = NULL;
00083             $this->key = NULL;
00084             $this->strValue = NULL;
00085         }
00086         $this->it->next();
00087     }

Here is the call graph for this function:

CachingIterator::currentCachingIterator::keyCachingIterator::valid

CachingIterator::rewind (  ) 

Rewind the Iterator.

Implements Iterator.

Reimplemented in RecursiveCachingIterator.

Definition at line 61 of file cachingiterator.inc.

References next().

00062     {
00063         $this->it->rewind();
00064         $this->next();
00065     }

Here is the call graph for this function:

CachingIterator::nextCachingIterator::currentCachingIterator::keyCachingIterator::valid

CachingIterator::valid (  ) 

Returns:
whether teh iterator is valid

Implements Iterator.

Definition at line 91 of file cachingiterator.inc.

Referenced by next().

00092     {
00093         return $this->valid;
00094     }


Member Data Documentation

CachingIterator::$current [private]

Definition at line 36 of file cachingiterator.inc.

CachingIterator::$it [private]

Definition at line 35 of file cachingiterator.inc.

Referenced by RecursiveCachingIterator::__construct(), and __construct().

CachingIterator::$key [private]

Definition at line 37 of file cachingiterator.inc.

CachingIterator::$strValue [private]

Definition at line 39 of file cachingiterator.inc.

CachingIterator::$valid [private]

Definition at line 38 of file cachingiterator.inc.

const CachingIterator::CALL_TOSTRING = 0x00000001

Definition at line 30 of file cachingiterator.inc.

Referenced by DirectoryTreeIterator::__construct(), and DirectoryGraphIterator::__construct().

const CachingIterator::CATCH_GET_CHILD = 0x00000002

Definition at line 31 of file cachingiterator.inc.

Referenced by DirectoryTreeIterator::__construct(), and DirectoryGraphIterator::__construct().

const CachingIterator::TOSTRING_USE_CURRENT = 0x00000020

Definition at line 33 of file cachingiterator.inc.

const CachingIterator::TOSTRING_USE_KEY = 0x00000010

Definition at line 32 of file cachingiterator.inc.


The documentation for this class was generated from the following file:
Generated on Thu Apr 26 01:05:10 2007 for SPL-StandardPHPLibrary by  doxygen 1.5.2