xref: /PHP-8.2/ext/spl/tests/iterator_034.phpt (revision b3e08881)
1--TEST--
2SPL: RecursiveIteratorIterator and break deep
3--FILE--
4<?php
5
6class MyRecursiveArrayIterator extends RecursiveArrayIterator
7{
8    function valid(): bool
9    {
10        if (!parent::valid())
11        {
12            echo __METHOD__ . "() = false\n";
13            return false;
14        }
15        else
16        {
17            return true;
18        }
19    }
20
21    function getChildren(): ?RecursiveArrayIterator
22    {
23        echo __METHOD__ . "()\n";
24        return parent::getChildren();
25    }
26
27    function rewind(): void
28    {
29        echo __METHOD__ . "()\n";
30        parent::rewind();
31    }
32}
33
34class RecursiveArrayIteratorIterator extends RecursiveIteratorIterator
35{
36    private $max_depth;
37    private $over = 0;
38
39    function __construct($it, $max_depth)
40    {
41        $this->max_depth = $max_depth;
42        parent::__construct($it);
43    }
44
45    function rewind(): void
46    {
47        echo __METHOD__ . "() - BEGIN\n";
48        parent::rewind();
49        echo __METHOD__ . "() - DONE\n";
50    }
51
52    function valid(): bool
53    {
54        echo __METHOD__ . "()\n";
55        return parent::valid();
56    }
57
58    function current(): mixed
59    {
60        echo __METHOD__ . "()\n";
61        return parent::current();
62    }
63
64    function key(): int
65    {
66        echo __METHOD__ . "()\n";
67        return parent::key();
68    }
69
70    function next(): void
71    {
72        echo __METHOD__ . "()\n";
73        parent::next();
74    }
75
76    function callHasChildren(): bool
77    {
78        $has = parent::callHasChildren();
79        $res = $this->getDepth() < $this->max_depth && $has;
80        echo __METHOD__ . "(".$this->getDepth().") = ".($res?"yes":"no")."/".($has?"yes":"no")."\n";
81        return $res;
82    }
83
84    function beginChildren(): void
85    {
86        echo __METHOD__ . "(".$this->getDepth().")\n";
87        parent::beginChildren();
88    }
89
90    function endChildren(): void
91    {
92        echo __METHOD__ . "(".$this->getDepth().")\n";
93        parent::endChildren();
94    }
95}
96
97$p = 0;
98$it = new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2);
99foreach($it as $k=>$v)
100{
101    if (is_array($v)) $v = join('',$v);
102    echo "$k=>$v\n";
103    if ($p++ == 5)
104    {
105        echo "===BREAK===\n";
106        break;
107    }
108}
109
110echo "===FOREND===\n";
111
112$it->rewind();
113
114echo "===CHECK===\n";
115
116var_dump($it->valid());
117var_dump($it->current() == "a");
118
119?>
120--EXPECT--
121RecursiveArrayIteratorIterator::rewind() - BEGIN
122MyRecursiveArrayIterator::rewind()
123RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
124RecursiveArrayIteratorIterator::rewind() - DONE
125RecursiveArrayIteratorIterator::valid()
126RecursiveArrayIteratorIterator::current()
127RecursiveArrayIteratorIterator::key()
1280=>a
129RecursiveArrayIteratorIterator::next()
130RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
131MyRecursiveArrayIterator::getChildren()
132MyRecursiveArrayIterator::rewind()
133RecursiveArrayIteratorIterator::beginChildren(1)
134RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
135RecursiveArrayIteratorIterator::valid()
136RecursiveArrayIteratorIterator::current()
137RecursiveArrayIteratorIterator::key()
1380=>ba
139RecursiveArrayIteratorIterator::next()
140RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
141MyRecursiveArrayIterator::getChildren()
142MyRecursiveArrayIterator::rewind()
143RecursiveArrayIteratorIterator::beginChildren(2)
144RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
145RecursiveArrayIteratorIterator::valid()
146RecursiveArrayIteratorIterator::current()
147RecursiveArrayIteratorIterator::key()
1480=>bba
149RecursiveArrayIteratorIterator::next()
150RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
151RecursiveArrayIteratorIterator::valid()
152RecursiveArrayIteratorIterator::current()
153RecursiveArrayIteratorIterator::key()
1541=>bbb
155RecursiveArrayIteratorIterator::next()
156MyRecursiveArrayIterator::valid() = false
157RecursiveArrayIteratorIterator::endChildren(2)
158RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
159MyRecursiveArrayIterator::getChildren()
160MyRecursiveArrayIterator::rewind()
161RecursiveArrayIteratorIterator::beginChildren(2)
162RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
163RecursiveArrayIteratorIterator::valid()
164RecursiveArrayIteratorIterator::current()
165RecursiveArrayIteratorIterator::key()
1660=>bcaa
167RecursiveArrayIteratorIterator::next()
168RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
169RecursiveArrayIteratorIterator::valid()
170RecursiveArrayIteratorIterator::current()
171RecursiveArrayIteratorIterator::key()
1721=>bcba
173===BREAK===
174===FOREND===
175RecursiveArrayIteratorIterator::rewind() - BEGIN
176RecursiveArrayIteratorIterator::endChildren(1)
177RecursiveArrayIteratorIterator::endChildren(0)
178MyRecursiveArrayIterator::rewind()
179RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
180RecursiveArrayIteratorIterator::rewind() - DONE
181===CHECK===
182RecursiveArrayIteratorIterator::valid()
183bool(true)
184RecursiveArrayIteratorIterator::current()
185bool(true)
186