xref: /PHP-5.6/ext/spl/tests/iterator_034.phpt (revision 610c7fbe)
1--TEST--
2SPL: RecursiveIteratorIterator and break deep
3--FILE--
4<?php
5
6class MyRecursiveArrayIterator extends RecursiveArrayIterator
7{
8	function valid()
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()
22	{
23		echo __METHOD__ . "()\n";
24		return parent::getChildren();
25	}
26
27	function rewind()
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()
46	{
47		echo __METHOD__ . "() - BEGIN\n";
48		parent::rewind();
49		echo __METHOD__ . "() - DONE\n";
50	}
51
52	function valid()
53	{
54		echo __METHOD__ . "()\n";
55		return parent::valid();
56	}
57
58	function current()
59	{
60		echo __METHOD__ . "()\n";
61		return parent::current();
62	}
63
64	function key()
65	{
66		echo __METHOD__ . "()\n";
67		return parent::key();
68	}
69
70	function next()
71	{
72		echo __METHOD__ . "()\n";
73		parent::next();
74	}
75
76	function callHasChildren()
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()
85	{
86		echo __METHOD__ . "(".$this->getDepth().")\n";
87		parent::beginChildren();
88	}
89
90	function endChildren()
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===DONE===
121<?php exit(0); ?>
122--EXPECT--
123RecursiveArrayIteratorIterator::rewind() - BEGIN
124MyRecursiveArrayIterator::rewind()
125RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
126RecursiveArrayIteratorIterator::rewind() - DONE
127RecursiveArrayIteratorIterator::valid()
128RecursiveArrayIteratorIterator::current()
129RecursiveArrayIteratorIterator::key()
1300=>a
131RecursiveArrayIteratorIterator::next()
132RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
133MyRecursiveArrayIterator::getChildren()
134MyRecursiveArrayIterator::rewind()
135RecursiveArrayIteratorIterator::beginChildren(1)
136RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
137RecursiveArrayIteratorIterator::valid()
138RecursiveArrayIteratorIterator::current()
139RecursiveArrayIteratorIterator::key()
1400=>ba
141RecursiveArrayIteratorIterator::next()
142RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
143MyRecursiveArrayIterator::getChildren()
144MyRecursiveArrayIterator::rewind()
145RecursiveArrayIteratorIterator::beginChildren(2)
146RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
147RecursiveArrayIteratorIterator::valid()
148RecursiveArrayIteratorIterator::current()
149RecursiveArrayIteratorIterator::key()
1500=>bba
151RecursiveArrayIteratorIterator::next()
152RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
153RecursiveArrayIteratorIterator::valid()
154RecursiveArrayIteratorIterator::current()
155RecursiveArrayIteratorIterator::key()
1561=>bbb
157RecursiveArrayIteratorIterator::next()
158MyRecursiveArrayIterator::valid() = false
159RecursiveArrayIteratorIterator::endChildren(2)
160RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
161MyRecursiveArrayIterator::getChildren()
162MyRecursiveArrayIterator::rewind()
163RecursiveArrayIteratorIterator::beginChildren(2)
164RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
165RecursiveArrayIteratorIterator::valid()
166RecursiveArrayIteratorIterator::current()
167RecursiveArrayIteratorIterator::key()
1680=>bcaa
169RecursiveArrayIteratorIterator::next()
170RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
171RecursiveArrayIteratorIterator::valid()
172RecursiveArrayIteratorIterator::current()
173RecursiveArrayIteratorIterator::key()
1741=>bcba
175===BREAK===
176===FOREND===
177RecursiveArrayIteratorIterator::rewind() - BEGIN
178RecursiveArrayIteratorIterator::endChildren(1)
179RecursiveArrayIteratorIterator::endChildren(0)
180MyRecursiveArrayIterator::rewind()
181RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
182RecursiveArrayIteratorIterator::rewind() - DONE
183===CHECK===
184RecursiveArrayIteratorIterator::valid()
185bool(true)
186RecursiveArrayIteratorIterator::current()
187bool(true)
188===DONE===
189