iterator_016.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. SPL: RecursiveIteratorIterator and beginChildren/endChildren
  3. --FILE--
  4. <?php
  5. class Menu extends ArrayObject
  6. {
  7. function getIterator(): RecursiveArrayIterator
  8. {
  9. echo __METHOD__ . "\n";
  10. return new RecursiveArrayIterator($this);
  11. }
  12. }
  13. class MenuOutput extends RecursiveIteratorIterator
  14. {
  15. function __construct(Menu $it)
  16. {
  17. parent::__construct($it);
  18. }
  19. function rewind(): void
  20. {
  21. echo "<ul>\n";
  22. parent::rewind();
  23. }
  24. function beginChildren(): void
  25. {
  26. echo str_repeat(' ',$this->getDepth())."<ul>\n";
  27. }
  28. function endChildren(): void
  29. {
  30. echo str_repeat(' ',$this->getDepth())."</ul>\n";
  31. }
  32. function valid(): bool
  33. {
  34. if (!parent::valid()) {
  35. echo "<ul>\n";
  36. return false;
  37. }
  38. return true;
  39. }
  40. }
  41. $arr = array("a", array("ba", array("bba", "bbb"), array(array("bcaa"))), array("ca"), "d");
  42. $obj = new Menu($arr);
  43. $rit = new MenuOutput($obj);
  44. foreach($rit as $k=>$v)
  45. {
  46. echo str_repeat(' ',$rit->getDepth()+1)."$k=>$v\n";
  47. }
  48. ?>
  49. --EXPECT--
  50. Menu::getIterator
  51. <ul>
  52. 0=>a
  53. <ul>
  54. 0=>ba
  55. <ul>
  56. 0=>bba
  57. 1=>bbb
  58. </ul>
  59. <ul>
  60. <ul>
  61. 0=>bcaa
  62. </ul>
  63. </ul>
  64. </ul>
  65. <ul>
  66. 0=>ca
  67. </ul>
  68. 3=>d
  69. <ul>