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