iterator_023.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and catch getChildren
  3. --FILE--
  4. <?php
  5. class MyRecursiveArrayIterator extends RecursiveArrayIterator
  6. {
  7. function getChildren()
  8. {
  9. echo __METHOD__ . "\n";
  10. return $this->current();
  11. }
  12. function valid()
  13. {
  14. if (!parent::valid())
  15. {
  16. echo __METHOD__ . " = false\n";
  17. return false;
  18. }
  19. else
  20. {
  21. return true;
  22. }
  23. }
  24. }
  25. class RecursiveArrayIteratorIterator extends RecursiveIteratorIterator
  26. {
  27. private $max_depth;
  28. private $over = 0;
  29. private $skip = false;
  30. function __construct($it, $max_depth)
  31. {
  32. $this->max_depth = $max_depth;
  33. parent::__construct($it, RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD);
  34. }
  35. function rewind()
  36. {
  37. echo __METHOD__ . "\n";
  38. $this->skip = false;
  39. parent::rewind();
  40. }
  41. function valid()
  42. {
  43. echo __METHOD__ . "\n";
  44. if ($this->skip)
  45. {
  46. $this->skip = false;
  47. $this->next();
  48. }
  49. return parent::valid();
  50. }
  51. function current()
  52. {
  53. echo __METHOD__ . "\n";
  54. return parent::current();
  55. }
  56. function key()
  57. {
  58. echo __METHOD__ . "\n";
  59. return parent::key();
  60. }
  61. function next()
  62. {
  63. echo __METHOD__ . "\n";
  64. parent::next();
  65. }
  66. function callHasChildren()
  67. {
  68. $this->skip = false;
  69. $has = parent::callHasChildren();
  70. $res = $this->getDepth() < $this->max_depth && $has;
  71. echo __METHOD__ . "(".$this->getDepth().") = ".($res?"yes":"no")."/".($has?"yes":"no")."\n";
  72. if ($has && !$res)
  73. {
  74. $this->over++;
  75. if ($this->over == 2) {
  76. $this->skip = true;
  77. }
  78. }
  79. return $res;
  80. }
  81. function callGetChildren()
  82. {
  83. if ($this->over == 2)
  84. {
  85. echo __METHOD__ . "(throw)\n";
  86. throw new Exception("Thrown in callGetChildren()");
  87. }
  88. echo __METHOD__ . "(ok:{$this->over})\n";
  89. return new MyRecursiveArrayIterator($this->current());
  90. }
  91. function beginChildren()
  92. {
  93. echo __METHOD__ . "(".$this->getDepth().")\n";
  94. }
  95. function endChildren()
  96. {
  97. echo __METHOD__ . "(".$this->getDepth().")\n";
  98. }
  99. }
  100. try
  101. {
  102. foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v)
  103. {
  104. if (is_array($v)) $v = join('',$v);
  105. echo "$k=>$v\n";
  106. }
  107. }
  108. catch(UnexpectedValueException $e)
  109. {
  110. echo $e->getMessage() . "\n";
  111. }
  112. ?>
  113. ===DONE===
  114. <?php exit(0); ?>
  115. --EXPECT--
  116. RecursiveArrayIteratorIterator::rewind
  117. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  118. RecursiveArrayIteratorIterator::valid
  119. RecursiveArrayIteratorIterator::current
  120. RecursiveArrayIteratorIterator::key
  121. 0=>a
  122. RecursiveArrayIteratorIterator::next
  123. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  124. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  125. RecursiveArrayIteratorIterator::current
  126. RecursiveArrayIteratorIterator::beginChildren(1)
  127. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  128. RecursiveArrayIteratorIterator::valid
  129. RecursiveArrayIteratorIterator::current
  130. RecursiveArrayIteratorIterator::key
  131. 0=>ba
  132. RecursiveArrayIteratorIterator::next
  133. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  134. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  135. RecursiveArrayIteratorIterator::current
  136. RecursiveArrayIteratorIterator::beginChildren(2)
  137. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  138. RecursiveArrayIteratorIterator::valid
  139. RecursiveArrayIteratorIterator::current
  140. RecursiveArrayIteratorIterator::key
  141. 0=>bba
  142. RecursiveArrayIteratorIterator::next
  143. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  144. RecursiveArrayIteratorIterator::valid
  145. RecursiveArrayIteratorIterator::current
  146. RecursiveArrayIteratorIterator::key
  147. 1=>bbb
  148. RecursiveArrayIteratorIterator::next
  149. MyRecursiveArrayIterator::valid = false
  150. RecursiveArrayIteratorIterator::endChildren(2)
  151. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  152. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  153. RecursiveArrayIteratorIterator::current
  154. RecursiveArrayIteratorIterator::beginChildren(2)
  155. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  156. RecursiveArrayIteratorIterator::valid
  157. RecursiveArrayIteratorIterator::current
  158. RecursiveArrayIteratorIterator::key
  159. 0=>bcaa
  160. RecursiveArrayIteratorIterator::next
  161. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  162. RecursiveArrayIteratorIterator::valid
  163. RecursiveArrayIteratorIterator::next
  164. MyRecursiveArrayIterator::valid = false
  165. RecursiveArrayIteratorIterator::endChildren(2)
  166. MyRecursiveArrayIterator::valid = false
  167. RecursiveArrayIteratorIterator::endChildren(1)
  168. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  169. RecursiveArrayIteratorIterator::callGetChildren(throw)
  170. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  171. RecursiveArrayIteratorIterator::current
  172. RecursiveArrayIteratorIterator::key
  173. 3=>d
  174. RecursiveArrayIteratorIterator::next
  175. MyRecursiveArrayIterator::valid = false
  176. RecursiveArrayIteratorIterator::valid
  177. MyRecursiveArrayIterator::valid = false
  178. ===DONE===