iterator_034.phpt 4.4 KB

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