SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Private Attributes | List of all members
CachingIterator Class Reference
Inheritance diagram for CachingIterator:
Inheritance graph
Collaboration diagram for CachingIterator:
Collaboration graph

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 unnecessary 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.

References $it, and next().

48  {
49  if ((($flags & self::CALL_TOSTRING) && ($flags & (self::TOSTRING_USE_KEY|self::TOSTRING_USE_CURRENT)))
50  || ((flags & (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)) == (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)))
51  {
52  throw new InvalidArgumentException('Flags must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, CIT_TOSTRING_USE_CURRENT');
53  }
54  $this->it = $it;
55  $this->flags = $flags & (0x0000FFFF);
56  $this->next();
57  }
next()
Forward to the next element.
Exception that denotes invalid arguments were passed.
Definition: spl.php:392

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.

123  {
124  return call_user_func_array(array($this->it, $func), $params);
125  }
CachingIterator::__toString ( )
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.

References $current, $key, and $strValue.

132  {
133  if ($this->flags & self::TOSTRING_USE_KEY)
134  {
135  return $this->key;
136  }
137  else if ($this->flags & self::TOSTRING_USE_CURRENT)
138  {
139  return $this->current;
140  }
141  if (!$this->flags & self::CALL_TOSTRING)
142  {
143  throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
144  }
145  return $this->strValue;
146  }
CachingIterator::current ( )
Returns
the current element

Implements Iterator.

Definition at line 105 of file cachingiterator.inc.

References $current.

Referenced by next().

106  {
107  return $this->current;
108  }
CachingIterator::getInnerIterator ( )
Returns
The inner iterator

Implements OuterIterator.

Definition at line 151 of file cachingiterator.inc.

References $it.

152  {
153  return $this->it;
154  }
CachingIterator::hasNext ( )
Returns
whether there is one more element

Definition at line 98 of file cachingiterator.inc.

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

Implements Iterator.

Definition at line 112 of file cachingiterator.inc.

References $key.

Referenced by next().

113  {
114  return $this->key;
115  }
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().

70  {
71  if ($this->valid = $this->it->valid()) {
72  $this->current = $this->it->current();
73  $this->key = $this->it->key();
74  if ($this->flags & self::CALL_TOSTRING) {
75  if (is_object($this->current)) {
76  $this->strValue = $this->current->__toString();
77  } else {
78  $this->strValue = (string)$this->current;
79  }
80  }
81  } else {
82  $this->current = NULL;
83  $this->key = NULL;
84  $this->strValue = NULL;
85  }
86  $this->it->next();
87  }

Here is the call graph for this function:

CachingIterator::rewind ( )

Rewind the Iterator.

Implements Iterator.

Definition at line 61 of file cachingiterator.inc.

References next().

62  {
63  $this->it->rewind();
64  $this->next();
65  }
next()
Forward to the next element.

Here is the call graph for this function:

CachingIterator::valid ( )
Returns
whether the iterator is valid

Implements Iterator.

Definition at line 91 of file cachingiterator.inc.

References $valid.

Referenced by next().

92  {
93  return $this->valid;
94  }

Member Data Documentation

CachingIterator::$current
private

Definition at line 36 of file cachingiterator.inc.

Referenced by __toString(), and current().

CachingIterator::$it
private

Definition at line 35 of file cachingiterator.inc.

Referenced by __construct(), and getInnerIterator().

CachingIterator::$key
private

Definition at line 37 of file cachingiterator.inc.

Referenced by __toString(), and key().

CachingIterator::$strValue
private

Definition at line 39 of file cachingiterator.inc.

Referenced by __toString().

CachingIterator::$valid
private

Definition at line 38 of file cachingiterator.inc.

Referenced by valid().

const CachingIterator::CALL_TOSTRING = 0x00000001
const CachingIterator::CATCH_GET_CHILD = 0x00000002
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: