iterator_021.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and hasChildren
  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. }
  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()
  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 beginChildren()
  82. {
  83. echo __METHOD__ . "(".$this->getDepth().")\n";
  84. }
  85. function endChildren()
  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. ===DONE===
  97. <?php exit(0); ?>
  98. --EXPECT--
  99. RecursiveArrayIteratorIterator::rewind
  100. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  101. RecursiveArrayIteratorIterator::valid
  102. RecursiveArrayIteratorIterator::current
  103. RecursiveArrayIteratorIterator::key
  104. 0=>a
  105. RecursiveArrayIteratorIterator::next
  106. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  107. MyRecursiveArrayIterator::getChildren
  108. RecursiveArrayIteratorIterator::beginChildren(1)
  109. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  110. RecursiveArrayIteratorIterator::valid
  111. RecursiveArrayIteratorIterator::current
  112. RecursiveArrayIteratorIterator::key
  113. 0=>ba
  114. RecursiveArrayIteratorIterator::next
  115. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  116. MyRecursiveArrayIterator::getChildren
  117. RecursiveArrayIteratorIterator::beginChildren(2)
  118. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  119. RecursiveArrayIteratorIterator::valid
  120. RecursiveArrayIteratorIterator::current
  121. RecursiveArrayIteratorIterator::key
  122. 0=>bba
  123. RecursiveArrayIteratorIterator::next
  124. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  125. RecursiveArrayIteratorIterator::valid
  126. RecursiveArrayIteratorIterator::current
  127. RecursiveArrayIteratorIterator::key
  128. 1=>bbb
  129. RecursiveArrayIteratorIterator::next
  130. MyRecursiveArrayIterator::valid = false
  131. RecursiveArrayIteratorIterator::endChildren(2)
  132. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  133. MyRecursiveArrayIterator::getChildren
  134. RecursiveArrayIteratorIterator::beginChildren(2)
  135. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  136. RecursiveArrayIteratorIterator::valid
  137. RecursiveArrayIteratorIterator::current
  138. RecursiveArrayIteratorIterator::key
  139. 0=>bcaa
  140. RecursiveArrayIteratorIterator::next
  141. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  142. RecursiveArrayIteratorIterator::valid
  143. RecursiveArrayIteratorIterator::next
  144. MyRecursiveArrayIterator::valid = false
  145. RecursiveArrayIteratorIterator::endChildren(2)
  146. MyRecursiveArrayIterator::valid = false
  147. RecursiveArrayIteratorIterator::endChildren(1)
  148. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  149. MyRecursiveArrayIterator::getChildren
  150. RecursiveArrayIteratorIterator::beginChildren(1)
  151. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  152. RecursiveArrayIteratorIterator::current
  153. RecursiveArrayIteratorIterator::key
  154. 0=>ca
  155. RecursiveArrayIteratorIterator::next
  156. MyRecursiveArrayIterator::valid = false
  157. RecursiveArrayIteratorIterator::endChildren(1)
  158. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  159. RecursiveArrayIteratorIterator::valid
  160. RecursiveArrayIteratorIterator::current
  161. RecursiveArrayIteratorIterator::key
  162. 3=>d
  163. RecursiveArrayIteratorIterator::next
  164. MyRecursiveArrayIterator::valid = false
  165. RecursiveArrayIteratorIterator::valid
  166. MyRecursiveArrayIterator::valid = false
  167. ===DONE===