Back to index

php5  5.3.10
Public Member Functions | Public Attributes | Private Attributes
CachingIterator Class Reference

Cached iteration over another Iterator. More...

Inheritance diagram for CachingIterator:
Inheritance graph
[legend]
Collaboration diagram for CachingIterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 __construct (Iterator $it, $flags=self::CALL_TOSTRING)
 Construct from another iterator.
 rewind ()
 Rewind the Iterator.
 next ()
 Forward to the next element.
 valid ()
 hasNext ()
 current ()
 key ()
 __call ($func, $params)
 Aggregate the inner iterator.
 __toString ()
 getInnerIterator ()

Public Attributes

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

Private Attributes

 $it
 $current
 $key
 $valid
 $strValue

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:
itIterator to cache
flagsBitmask:
  • CALL_TOSTRING (whether to call __toString() for every element)

Definition at line 47 of file cachingiterator.inc.

       {
              if ((($flags & self::CALL_TOSTRING) && ($flags & (self::TOSTRING_USE_KEY|self::TOSTRING_USE_CURRENT)))
              || ((flags & (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)) == (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)))
              {
                     throw new InvalidArgumentException('Flags must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, CIT_TOSTRING_USE_CURRENT');
              }
              $this->it = $it;
              $this->flags = $flags & (0x0000FFFF);
              $this->next();
       }

Here is the call graph for this function:


Member Function Documentation

CachingIterator::__call ( func,
params 
)

Aggregate the inner iterator.

Parameters:
funcName of method to invoke
paramsArray of parameters to pass to method

Definition at line 122 of file cachingiterator.inc.

       {
              return call_user_func_array(array($this->it, $func), $params);
       }
Returns:
the string represenatation that was generated for the current element
Exceptions:
exceptionwhen CALL_TOSTRING was not specified in constructor

Definition at line 131 of file cachingiterator.inc.

       {
              if ($this->flags & self::TOSTRING_USE_KEY)
              {
                     return $this->key;
              }
              else if ($this->flags & self::TOSTRING_USE_CURRENT)
              {
                     return $this->current;
              }
              if (!$this->flags & self::CALL_TOSTRING)
              {
                     throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
              }
              return $this->strValue;
       }
Returns:
the current element

Implements Iterator.

Definition at line 105 of file cachingiterator.inc.

       {
              return $this->current;
       }

Here is the caller graph for this function:

Returns:
The inner iterator

Implements OuterIterator.

Definition at line 151 of file cachingiterator.inc.

       {
              return $this->it;
       }
Returns:
whether there is one more element

Definition at line 98 of file cachingiterator.inc.

       {
              return $this->it->valid();
       }
Returns:
the current key

Implements Iterator.

Definition at line 112 of file cachingiterator.inc.

       {
              return $this->key;
       }

Here is the caller graph for this function:

Forward to the next element.

Implements Iterator.

Definition at line 69 of file cachingiterator.inc.

       {
              if ($this->valid = $this->it->valid()) {
                     $this->current = $this->it->current();
                     $this->key = $this->it->key();
                     if ($this->flags & self::CALL_TOSTRING) {
                            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 = NULL;
              }
              $this->it->next();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Rewind the Iterator.

Implements Iterator.

Reimplemented in RecursiveCachingIterator.

Definition at line 61 of file cachingiterator.inc.

       {
              $this->it->rewind();
              $this->next();
       }

Here is the call graph for this function:

Returns:
whether the iterator is valid

Implements Iterator.

Definition at line 91 of file cachingiterator.inc.

       {
              return $this->valid;
       }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 36 of file cachingiterator.inc.

Definition at line 35 of file cachingiterator.inc.

Definition at line 37 of file cachingiterator.inc.

Definition at line 39 of file cachingiterator.inc.

Definition at line 38 of file cachingiterator.inc.

const CachingIterator::CALL_TOSTRING = 0x00000001

Definition at line 30 of file cachingiterator.inc.

Definition at line 31 of file cachingiterator.inc.

Definition at line 33 of file cachingiterator.inc.

Definition at line 32 of file cachingiterator.inc.


The documentation for this class was generated from the following file: