iterator_023.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and catch getChildren
  3. --FILE--
  4. <?php
  5. class MyRecursiveArrayIterator extends RecursiveArrayIterator
  6. {
  7. function getChildren(): ?RecursiveArrayIterator
  8. {
  9. echo __METHOD__ . "\n";
  10. return $this->current();
  11. }
  12. function valid(): bool
  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(): 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(): int
  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 callGetChildren(): MyRecursiveArrayIterator
  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(): void
  92. {
  93. echo __METHOD__ . "(".$this->getDepth().")\n";
  94. }
  95. function endChildren(): void
  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. --EXPECT--
  114. RecursiveArrayIteratorIterator::rewind
  115. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  116. RecursiveArrayIteratorIterator::valid
  117. RecursiveArrayIteratorIterator::current
  118. RecursiveArrayIteratorIterator::key
  119. 0=>a
  120. RecursiveArrayIteratorIterator::next
  121. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  122. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  123. RecursiveArrayIteratorIterator::current
  124. RecursiveArrayIteratorIterator::beginChildren(1)
  125. RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
  126. RecursiveArrayIteratorIterator::valid
  127. RecursiveArrayIteratorIterator::current
  128. RecursiveArrayIteratorIterator::key
  129. 0=>ba
  130. RecursiveArrayIteratorIterator::next
  131. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  132. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  133. RecursiveArrayIteratorIterator::current
  134. RecursiveArrayIteratorIterator::beginChildren(2)
  135. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  136. RecursiveArrayIteratorIterator::valid
  137. RecursiveArrayIteratorIterator::current
  138. RecursiveArrayIteratorIterator::key
  139. 0=>bba
  140. RecursiveArrayIteratorIterator::next
  141. RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
  142. RecursiveArrayIteratorIterator::valid
  143. RecursiveArrayIteratorIterator::current
  144. RecursiveArrayIteratorIterator::key
  145. 1=>bbb
  146. RecursiveArrayIteratorIterator::next
  147. MyRecursiveArrayIterator::valid = false
  148. RecursiveArrayIteratorIterator::endChildren(2)
  149. RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
  150. RecursiveArrayIteratorIterator::callGetChildren(ok:0)
  151. RecursiveArrayIteratorIterator::current
  152. RecursiveArrayIteratorIterator::beginChildren(2)
  153. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  154. RecursiveArrayIteratorIterator::valid
  155. RecursiveArrayIteratorIterator::current
  156. RecursiveArrayIteratorIterator::key
  157. 0=>bcaa
  158. RecursiveArrayIteratorIterator::next
  159. RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
  160. RecursiveArrayIteratorIterator::valid
  161. RecursiveArrayIteratorIterator::next
  162. MyRecursiveArrayIterator::valid = false
  163. RecursiveArrayIteratorIterator::endChildren(2)
  164. MyRecursiveArrayIterator::valid = false
  165. RecursiveArrayIteratorIterator::endChildren(1)
  166. RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
  167. RecursiveArrayIteratorIterator::callGetChildren(throw)
  168. RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
  169. RecursiveArrayIteratorIterator::current
  170. RecursiveArrayIteratorIterator::key
  171. 3=>d
  172. RecursiveArrayIteratorIterator::next
  173. MyRecursiveArrayIterator::valid = false
  174. RecursiveArrayIteratorIterator::valid
  175. MyRecursiveArrayIterator::valid = false