iterator_021.phpt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and hasChildren
  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. }
  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);
  34. }
  35. function rewind(): void
  36. {
  37. echo __METHOD__ . "\n";
  38. $this->skip = false;
  39. parent::rewind();
  40. }
  41. function valid(): bool
  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(): mixed
  52. {
  53. echo __METHOD__ . "\n";
  54. return parent::current();
  55. }
  56. function key(): mixed
  57. {
  58. echo __METHOD__ . "\n";
  59. return parent::key();
  60. }
  61. function next(): void
  62. {
  63. echo __METHOD__ . "\n";
  64. parent::next();
  65. }
  66. function callHasChildren(): bool
  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 beginChildren(): void
  82. {
  83. echo __METHOD__ . "(".$this->getDepth().")\n";
  84. }
  85. function endChildren(): void
  86. {
  87. echo __METHOD__ . "(".$this->getDepth().")\n";
  88. }
  89. }
  90. foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v)
  91. {
  92. if (is_array($v)) $v = join('',$v);
  93. echo "$k=>$v\n";
  94. }
  95. ?>
  96. --EXPECT--
  97. RecursiveArrayIteratorIterator::rewind
  98. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  99. RecursiveArrayIteratorIterator::valid
  100. RecursiveArrayIteratorIterator::current
  101. RecursiveArrayIteratorIterator::key
  102. 0=>a
  103. RecursiveArrayIteratorIterator::next
  104. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  105. MyRecursiveArrayIterator::getChildren
  106. RecursiveArrayIteratorIterator::beginChildren(1)
  107. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  108. RecursiveArrayIteratorIterator::valid
  109. RecursiveArrayIteratorIterator::current
  110. RecursiveArrayIteratorIterator::key
  111. 0=>ba
  112. RecursiveArrayIteratorIterator::next
  113. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  114. MyRecursiveArrayIterator::getChildren
  115. RecursiveArrayIteratorIterator::beginChildren(2)
  116. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  117. RecursiveArrayIteratorIterator::valid
  118. RecursiveArrayIteratorIterator::current
  119. RecursiveArrayIteratorIterator::key
  120. 0=>bba
  121. RecursiveArrayIteratorIterator::next
  122. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  123. RecursiveArrayIteratorIterator::valid
  124. RecursiveArrayIteratorIterator::current
  125. RecursiveArrayIteratorIterator::key
  126. 1=>bbb
  127. RecursiveArrayIteratorIterator::next
  128. MyRecursiveArrayIterator::valid = false
  129. RecursiveArrayIteratorIterator::endChildren(2)
  130. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  131. MyRecursiveArrayIterator::getChildren
  132. RecursiveArrayIteratorIterator::beginChildren(2)
  133. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  134. RecursiveArrayIteratorIterator::valid
  135. RecursiveArrayIteratorIterator::current
  136. RecursiveArrayIteratorIterator::key
  137. 0=>bcaa
  138. RecursiveArrayIteratorIterator::next
  139. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  140. RecursiveArrayIteratorIterator::valid
  141. RecursiveArrayIteratorIterator::next
  142. MyRecursiveArrayIterator::valid = false
  143. RecursiveArrayIteratorIterator::endChildren(2)
  144. MyRecursiveArrayIterator::valid = false
  145. RecursiveArrayIteratorIterator::endChildren(1)
  146. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  147. MyRecursiveArrayIterator::getChildren
  148. RecursiveArrayIteratorIterator::beginChildren(1)
  149. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  150. RecursiveArrayIteratorIterator::current
  151. RecursiveArrayIteratorIterator::key
  152. 0=>ca
  153. RecursiveArrayIteratorIterator::next
  154. MyRecursiveArrayIterator::valid = false
  155. RecursiveArrayIteratorIterator::endChildren(1)
  156. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  157. RecursiveArrayIteratorIterator::valid
  158. RecursiveArrayIteratorIterator::current
  159. RecursiveArrayIteratorIterator::key
  160. 3=>d
  161. RecursiveArrayIteratorIterator::next
  162. MyRecursiveArrayIterator::valid = false
  163. RecursiveArrayIteratorIterator::valid
  164. MyRecursiveArrayIterator::valid = false