multipleiterator.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /** @file multipleiterator.inc
  3. * @ingroup SPL
  4. * @brief class MultipleIterator
  5. * @author Johannes Schlueter
  6. * @author Marcus Boerger
  7. * @date 2008 - 2009
  8. *
  9. * SPL - Standard PHP Library
  10. */
  11. /** @ingroup SPL
  12. * @brief Iterator that iterates over several iterators one after the other
  13. * @author Johannes Schlueter
  14. * @author Marcus Boerger
  15. * @version 1.0
  16. * @since PHP 5.3
  17. */
  18. class MultipleIterator implements Iterator
  19. {
  20. /** Inner Iterators */
  21. private $iterators;
  22. /** Flags: const MIT_* */
  23. private $flags;
  24. /** do not require all sub iterators to be valid in iteration */
  25. const MIT_NEED_ANY = 0;
  26. /** require all sub iterators to be valid in iteration */
  27. const MIT_NEED_ALL = 1;
  28. /** keys are created from sub iterators position */
  29. const MIT_KEYS_NUMERIC = 0;
  30. /** keys are created from sub iterators associated infromation */
  31. const MIT_KEYS_ASSOC = 2;
  32. /** Construct a new empty MultipleIterator
  33. * @param flags MIT_* flags
  34. */
  35. public function __construct($flags = self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)
  36. {
  37. $this->iterators = new SplObjectStorage();
  38. $this->flags = $flags;
  39. }
  40. /** @return current flags MIT_* */
  41. public function getFlags()
  42. {
  43. return $this->flags;
  44. }
  45. /** @param $flags new flags. */
  46. public function setFlags($flags)
  47. {
  48. $this->flags = $flags;
  49. }
  50. /** @param $iter new Iterator to attach.
  51. * @param $inf associative info forIteraotr, must be NULL, integer or string
  52. *
  53. * @throws IllegalValueException if a inf is none of NULL, integer or string
  54. * @throws IllegalValueException if a inf is already an associated info
  55. */
  56. public function attachIterator(Iterator $iter, $inf = NULL)
  57. {
  58. if (!is_null($inf))
  59. {
  60. if (!is_int($inf) && !is_string($inf))
  61. {
  62. throw new IllegalValueException('Inf must be NULL, integer or string');
  63. }
  64. foreach($this->iterators as $iter)
  65. {
  66. if ($inf == $this->iterators->getInfo())
  67. {
  68. throw new IllegalValueException('Key duplication error');
  69. }
  70. }
  71. }
  72. $this->iterators->attach($iter, $inf);
  73. }
  74. /** @param $iter attached Iterator that should be detached. */
  75. public function detachIterator(Iterator $iter)
  76. {
  77. $this->iterators->detach($iter);
  78. }
  79. /** @param $iter Iterator to check
  80. * @return whether $iter is attached or not
  81. */
  82. public function containsIterator(Iterator $iter)
  83. {
  84. return $this->iterator->contains($iter);
  85. }
  86. /** @return number of attached Iterator instances. */
  87. public function countIterators()
  88. {
  89. return $this->iterators->count();
  90. }
  91. /** Rewind all attached Iterator instances. */
  92. public function rewind()
  93. {
  94. foreach($this->iterators as $iter)
  95. {
  96. $iter->rewind();
  97. }
  98. }
  99. /**
  100. * @return whether all or one sub iterator is valid depending on flags.
  101. * In mode MIT_NEED_ALL we expect all sub iterators to be valid and
  102. * return flase on the first non valid one. If that flag is not set we
  103. * return true on the first valid sub iterator found. If no Iterator
  104. * is attached, we always return false.
  105. */
  106. public function valid()
  107. {
  108. if (!sizeof($this->iterators)) {
  109. return false;
  110. }
  111. // The following code is an optimized version that executes as few
  112. // valid() calls as necessary and that only checks the flags once.
  113. $expect = $this->flags & self::MIT_NEED_ALL ? true : false;
  114. foreach($this->iterators as $iter)
  115. {
  116. if ($expect != $iter->valid())
  117. {
  118. return !$expect;
  119. }
  120. }
  121. return $expect;
  122. }
  123. /** Move all attached Iterator instances forward. That is invoke
  124. * their next() method regardless of their state.
  125. */
  126. public function next()
  127. {
  128. foreach($this->iterators as $iter)
  129. {
  130. $iter->next();
  131. }
  132. }
  133. /** @return false if no sub Iterator is attached and an array of
  134. * all registered Iterator instances current() result.
  135. * @throws RuntimeException if mode MIT_NEED_ALL is set and at least one
  136. * attached Iterator is not valid().
  137. * @throws IllegalValueException if a key is NULL and MIT_KEYS_ASSOC is set.
  138. */
  139. public function current()
  140. {
  141. if (!sizeof($this->iterators))
  142. {
  143. return false;
  144. }
  145. $retval = array();
  146. foreach($this->iterators as $iter)
  147. {
  148. if ($iter->valid())
  149. {
  150. if ($this->flags & self::MIT_KEYS_ASSOC)
  151. {
  152. $key = $this->iterators->getInfo();
  153. if (is_null($key))
  154. {
  155. throw new IllegalValueException('Sub-Iterator is associated with NULL');
  156. }
  157. $retval[$key] = $iter->current();
  158. }
  159. else
  160. {
  161. $retval[] = $iter->current();
  162. }
  163. }
  164. else if ($this->flags & self::MIT_NEED_ALL)
  165. {
  166. throw new RuntimeException('Called current() with non valid sub iterator');
  167. }
  168. else
  169. {
  170. $retval[] = NULL;
  171. }
  172. }
  173. return $retval;
  174. }
  175. /** @return false if no sub Iterator is attached and an array of
  176. * all registered Iterator instances key() result.
  177. * @throws LogicException if mode MIT_NEED_ALL is set and at least one
  178. * attached Iterator is not valid().
  179. */
  180. public function key()
  181. {
  182. if (!sizeof($this->iterators))
  183. {
  184. return false;
  185. }
  186. $retval = array();
  187. foreach($this->iterators as $iter)
  188. {
  189. if ($iter->valid())
  190. {
  191. $retval[] = $iter->key();
  192. }
  193. else if ($this->flags & self::MIT_NEED_ALL)
  194. {
  195. throw new LogicException('Called key() with non valid sub iterator');
  196. }
  197. else
  198. {
  199. $retval[] = NULL;
  200. }
  201. }
  202. return $retval;
  203. }
  204. }