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

Public Member Functions

 __construct ($flags=self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)
 
 attachIterator (Iterator $iter, $inf=NULL)
 
 containsIterator (Iterator $iter)
 
 countIterators ()
 
 current ()
 
 detachIterator (Iterator $iter)
 
 getFlags ()
 
 key ()
 
 next ()
 
 rewind ()
 
 setFlags ($flags)
 
 valid ()
 

Public Attributes

const MIT_KEYS_ASSOC = 2
 
const MIT_KEYS_NUMERIC = 0
 
const MIT_NEED_ALL = 1
 
const MIT_NEED_ANY = 0
 

Private Attributes

 $flags
 
 $iterators
 

Detailed Description

Iterator that iterates over several iterators one after the other.

Author
Johannes Schlueter
Marcus Boerger
Version
1.0
Since
PHP 5.3

Definition at line 19 of file multipleiterator.inc.

Constructor & Destructor Documentation

MultipleIterator::__construct (   $flags = self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)

Construct a new empty MultipleIterator.

Parameters
flagsMIT_* flags

Definition at line 42 of file multipleiterator.inc.

References $flags.

43  {
44  $this->iterators = new SplObjectStorage();
45  $this->flags = $flags;
46  }
$flags
Flags: const MIT_*.
Object storage.

Member Function Documentation

MultipleIterator::attachIterator ( Iterator  $iter,
  $inf = NULL 
)
Parameters
$iternew Iterator to attach.
$infassociative info forIteraotr, must be NULL, integer or string
Exceptions
IllegalValueExceptionif a inf is none of NULL, integer or string
IllegalValueExceptionif a inf is already an associated info

Definition at line 66 of file multipleiterator.inc.

67  {
68 
69  if (!is_null($inf))
70  {
71  if (!is_int($inf) && !is_string($inf))
72  {
73  throw new IllegalValueException('Inf must be NULL, integer or string');
74  }
75  foreach($this->iterators as $iter)
76  {
77  if ($inf == $this->iterators->getInfo())
78  {
79  throw new IllegalValueException('Key duplication error');
80  }
81  }
82  }
83  $this->iterators->attach($iter, $inf);
84  }
MultipleIterator::containsIterator ( Iterator  $iter)
Parameters
$iterIterator to check
Returns
whether $iter is attached or not

Definition at line 95 of file multipleiterator.inc.

96  {
97  return $this->iterator->contains($iter);
98  }
MultipleIterator::countIterators ( )
Returns
number of attached Iterator instances.

Definition at line 101 of file multipleiterator.inc.

102  {
103  return $this->iterators->count();
104  }
MultipleIterator::current ( )
Returns
false if no sub Iterator is attached and an array of all registered Iterator instances current() result.
Exceptions
RuntimeExceptionif mode MIT_NEED_ALL is set and at least one attached Iterator is not valid().
IllegalValueExceptionif a key is NULL and MIT_KEYS_ASSOC is set.

Implements Iterator.

Definition at line 157 of file multipleiterator.inc.

158  {
159  if (!sizeof($this->iterators))
160  {
161  return false;
162  }
163  $retval = array();
164  foreach($this->iterators as $iter)
165  {
166  if ($iter->valid())
167  {
168  if ($this->flags & self::MIT_KEYS_ASSOC)
169  {
170  $key = $this->iterators->getInfo();
171  if (is_null($key))
172  {
173  throw new IllegalValueException('Sub-Iterator is associated with NULL');
174  }
175  $retval[$key] = $iter->current();
176  }
177  else
178  {
179  $retval[] = $iter->current();
180  }
181  }
182  else if ($this->flags & self::MIT_NEED_ALL)
183  {
184  throw new RuntimeException('Called current() with non valid sub iterator');
185  }
186  else
187  {
188  $retval[] = NULL;
189  }
190  }
191  return $retval;
192  }
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423
MultipleIterator::detachIterator ( Iterator  $iter)
Parameters
$iterattached Iterator that should be detached.

Definition at line 87 of file multipleiterator.inc.

88  {
89  $this->iterators->detach($iter);
90  }
MultipleIterator::getFlags ( )
Returns
current flags MIT_*

Definition at line 49 of file multipleiterator.inc.

References $flags.

50  {
51  return $this->flags;
52  }
$flags
Flags: const MIT_*.
MultipleIterator::key ( )
Returns
false if no sub Iterator is attached and an array of all registered Iterator instances key() result.
Exceptions
LogicExceptionif mode MIT_NEED_ALL is set and at least one attached Iterator is not valid().

Implements Iterator.

Definition at line 199 of file multipleiterator.inc.

200  {
201  if (!sizeof($this->iterators))
202  {
203  return false;
204  }
205  $retval = array();
206  foreach($this->iterators as $iter)
207  {
208  if ($iter->valid())
209  {
210  $retval[] = $iter->key();
211  }
212  else if ($this->flags & self::MIT_NEED_ALL)
213  {
214  throw new LogicException('Called key() with non valid sub iterator');
215  }
216  else
217  {
218  $retval[] = NULL;
219  }
220  }
221  return $retval;
222  }
Exception that represents error in the program logic.
Definition: spl.php:353
MultipleIterator::next ( )

Move all attached Iterator instances forward.

That is invoke their next() method regardless of their state.

Implements Iterator.

Definition at line 143 of file multipleiterator.inc.

144  {
145  foreach($this->iterators as $iter)
146  {
147  $iter->next();
148  }
149  }
MultipleIterator::rewind ( )

Rewind all attached Iterator instances.

Implements Iterator.

Definition at line 107 of file multipleiterator.inc.

108  {
109  foreach($this->iterators as $iter)
110  {
111  $iter->rewind();
112  }
113  }
MultipleIterator::setFlags (   $flags)
Parameters
$flagsnew flags.

Definition at line 55 of file multipleiterator.inc.

References $flags.

56  {
57  $this->flags = $flags;
58  }
$flags
Flags: const MIT_*.
MultipleIterator::valid ( )
Returns
whether all or one sub iterator is valid depending on flags. In mode MIT_NEED_ALL we expect all sub iterators to be valid and return flase on the first non valid one. If that flag is not set we return true on the first valid sub iterator found. If no Iterator is attached, we always return false.

Implements Iterator.

Definition at line 122 of file multipleiterator.inc.

123  {
124  if (!sizeof($this->iterators)) {
125  return false;
126  }
127  // The following code is an optimized version that executes as few
128  // valid() calls as necessary and that only checks the flags once.
129  $expect = $this->flags & self::MIT_NEED_ALL ? true : false;
130  foreach($this->iterators as $iter)
131  {
132  if ($expect != $iter->valid())
133  {
134  return !$expect;
135  }
136  }
137  return $expect;
138  }

Member Data Documentation

MultipleIterator::$flags
private

Flags: const MIT_*.

Definition at line 25 of file multipleiterator.inc.

Referenced by __construct(), getFlags(), and setFlags().

MultipleIterator::$iterators
private

Inner Iterators.

Definition at line 22 of file multipleiterator.inc.

const MultipleIterator::MIT_KEYS_ASSOC = 2

keys are created from sub iterators associated infromation

Definition at line 37 of file multipleiterator.inc.

const MultipleIterator::MIT_KEYS_NUMERIC = 0

keys are created from sub iterators position

Definition at line 34 of file multipleiterator.inc.

const MultipleIterator::MIT_NEED_ALL = 1

require all sub iterators to be valid in iteration

Definition at line 31 of file multipleiterator.inc.

const MultipleIterator::MIT_NEED_ANY = 0

do not require all sub iterators to be valid in iteration

Definition at line 28 of file multipleiterator.inc.


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