SPL-StandardPHPLibrary
cachingiterator.inc
Go to the documentation of this file.
1 <?php
2 
29 {
30  const CALL_TOSTRING = 0x00000001;
31  const CATCH_GET_CHILD = 0x00000002;
32  const TOSTRING_USE_KEY = 0x00000010;
33  const TOSTRING_USE_CURRENT = 0x00000020;
34 
35  private $it;
36  private $current;
37  private $key;
38  private $valid;
39  private $strValue;
40 
47  function __construct(Iterator $it, $flags = self::CALL_TOSTRING)
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  }
58 
61  function rewind()
62  {
63  $this->it->rewind();
64  $this->next();
65  }
66 
69  function next()
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  }
88 
91  function valid()
92  {
93  return $this->valid;
94  }
95 
98  function hasNext()
99  {
100  return $this->it->valid();
101  }
102 
105  function current()
106  {
107  return $this->current;
108  }
109 
112  function key()
113  {
114  return $this->key;
115  }
116 
122  function __call($func, $params)
123  {
124  return call_user_func_array(array($this->it, $func), $params);
125  }
126 
131  function __toString()
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  }
147 
151  function getInnerIterator()
152  {
153  return $this->it;
154  }
155 }
156 
157 ?>
__call($func, $params)
Aggregate the inner iterator.
Interface to access the current inner iteraor of iterator wrappers.
Cached iteration over another Iterator.
next()
Forward to the next element.
Basic iterator.
Definition: spl.php:549
__construct(Iterator $it, $flags=self::CALL_TOSTRING)
Construct from another iterator.
Exception that denotes invalid arguments were passed.
Definition: spl.php:392
rewind()
Rewind the Iterator.