iterator_034.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and break deep
  3. --FILE--
  4. <?php
  5. class MyRecursiveArrayIterator extends RecursiveArrayIterator
  6. {
  7. function valid(): bool
  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(): ?RecursiveArrayIterator
  20. {
  21. echo __METHOD__ . "()\n";
  22. return parent::getChildren();
  23. }
  24. function rewind(): void
  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(): void
  40. {
  41. echo __METHOD__ . "() - BEGIN\n";
  42. parent::rewind();
  43. echo __METHOD__ . "() - DONE\n";
  44. }
  45. function valid(): bool
  46. {
  47. echo __METHOD__ . "()\n";
  48. return parent::valid();
  49. }
  50. function current(): mixed
  51. {
  52. echo __METHOD__ . "()\n";
  53. return parent::current();
  54. }
  55. function key(): int
  56. {
  57. echo __METHOD__ . "()\n";
  58. return parent::key();
  59. }
  60. function next(): void
  61. {
  62. echo __METHOD__ . "()\n";
  63. parent::next();
  64. }
  65. function callHasChildren(): bool
  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(): void
  73. {
  74. echo __METHOD__ . "(".$this->getDepth().")\n";
  75. parent::beginChildren();
  76. }
  77. function endChildren(): void
  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. --EXPECT--
  102. RecursiveArrayIteratorIterator::rewind() - BEGIN
  103. MyRecursiveArrayIterator::rewind()
  104. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  105. RecursiveArrayIteratorIterator::rewind() - DONE
  106. RecursiveArrayIteratorIterator::valid()
  107. RecursiveArrayIteratorIterator::current()
  108. RecursiveArrayIteratorIterator::key()
  109. 0=>a
  110. RecursiveArrayIteratorIterator::next()
  111. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  112. MyRecursiveArrayIterator::getChildren()
  113. MyRecursiveArrayIterator::rewind()
  114. RecursiveArrayIteratorIterator::beginChildren(1)
  115. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  116. RecursiveArrayIteratorIterator::valid()
  117. RecursiveArrayIteratorIterator::current()
  118. RecursiveArrayIteratorIterator::key()
  119. 0=>ba
  120. RecursiveArrayIteratorIterator::next()
  121. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  122. MyRecursiveArrayIterator::getChildren()
  123. MyRecursiveArrayIterator::rewind()
  124. RecursiveArrayIteratorIterator::beginChildren(2)
  125. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  126. RecursiveArrayIteratorIterator::valid()
  127. RecursiveArrayIteratorIterator::current()
  128. RecursiveArrayIteratorIterator::key()
  129. 0=>bba
  130. RecursiveArrayIteratorIterator::next()
  131. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  132. RecursiveArrayIteratorIterator::valid()
  133. RecursiveArrayIteratorIterator::current()
  134. RecursiveArrayIteratorIterator::key()
  135. 1=>bbb
  136. RecursiveArrayIteratorIterator::next()
  137. MyRecursiveArrayIterator::valid() = false
  138. RecursiveArrayIteratorIterator::endChildren(2)
  139. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  140. MyRecursiveArrayIterator::getChildren()
  141. MyRecursiveArrayIterator::rewind()
  142. RecursiveArrayIteratorIterator::beginChildren(2)
  143. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  144. RecursiveArrayIteratorIterator::valid()
  145. RecursiveArrayIteratorIterator::current()
  146. RecursiveArrayIteratorIterator::key()
  147. 0=>bcaa
  148. RecursiveArrayIteratorIterator::next()
  149. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  150. RecursiveArrayIteratorIterator::valid()
  151. RecursiveArrayIteratorIterator::current()
  152. RecursiveArrayIteratorIterator::key()
  153. 1=>bcba
  154. ===BREAK===
  155. ===FOREND===
  156. RecursiveArrayIteratorIterator::rewind() - BEGIN
  157. RecursiveArrayIteratorIterator::endChildren(1)
  158. RecursiveArrayIteratorIterator::endChildren(0)
  159. MyRecursiveArrayIterator::rewind()
  160. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  161. RecursiveArrayIteratorIterator::rewind() - DONE
  162. ===CHECK===
  163. RecursiveArrayIteratorIterator::valid()
  164. bool(true)
  165. RecursiveArrayIteratorIterator::current()
  166. bool(true)