xref: /PHP-5.6/ext/spl/tests/iterator_021.phpt (revision 610c7fbe)
1--TEST--
2SPL: RecursiveIteratorIterator and hasChildren
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
28class RecursiveArrayIteratorIterator extends RecursiveIteratorIterator
29{
30	private $max_depth;
31	private $over = 0;
32	private $skip = false;
33
34	function __construct($it, $max_depth)
35	{
36		$this->max_depth = $max_depth;
37		parent::__construct($it);
38	}
39
40	function rewind()
41	{
42		echo __METHOD__ . "\n";
43		$this->skip = false;
44		parent::rewind();
45	}
46
47	function valid()
48	{
49		echo __METHOD__ . "\n";
50		if ($this->skip)
51		{
52			$this->skip = false;
53			$this->next();
54		}
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		$this->skip = false;
79		$has = parent::callHasChildren();
80		$res = $this->getDepth() < $this->max_depth && $has;
81		echo __METHOD__ . "(".$this->getDepth().") = ".($res?"yes":"no")."/".($has?"yes":"no")."\n";
82		if ($has && !$res)
83		{
84			$this->over++;
85			if ($this->over == 2) {
86				$this->skip = true;
87			}
88		}
89		return $res;
90	}
91
92	function beginChildren()
93	{
94		echo __METHOD__ . "(".$this->getDepth().")\n";
95	}
96
97	function endChildren()
98	{
99		echo __METHOD__ . "(".$this->getDepth().")\n";
100	}
101}
102
103foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v)
104{
105	if (is_array($v)) $v = join('',$v);
106	echo "$k=>$v\n";
107}
108?>
109===DONE===
110<?php exit(0); ?>
111--EXPECT--
112RecursiveArrayIteratorIterator::rewind
113RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
114RecursiveArrayIteratorIterator::valid
115RecursiveArrayIteratorIterator::current
116RecursiveArrayIteratorIterator::key
1170=>a
118RecursiveArrayIteratorIterator::next
119RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
120MyRecursiveArrayIterator::getChildren
121RecursiveArrayIteratorIterator::beginChildren(1)
122RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
123RecursiveArrayIteratorIterator::valid
124RecursiveArrayIteratorIterator::current
125RecursiveArrayIteratorIterator::key
1260=>ba
127RecursiveArrayIteratorIterator::next
128RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
129MyRecursiveArrayIterator::getChildren
130RecursiveArrayIteratorIterator::beginChildren(2)
131RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
132RecursiveArrayIteratorIterator::valid
133RecursiveArrayIteratorIterator::current
134RecursiveArrayIteratorIterator::key
1350=>bba
136RecursiveArrayIteratorIterator::next
137RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
138RecursiveArrayIteratorIterator::valid
139RecursiveArrayIteratorIterator::current
140RecursiveArrayIteratorIterator::key
1411=>bbb
142RecursiveArrayIteratorIterator::next
143MyRecursiveArrayIterator::valid = false
144RecursiveArrayIteratorIterator::endChildren(2)
145RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
146MyRecursiveArrayIterator::getChildren
147RecursiveArrayIteratorIterator::beginChildren(2)
148RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
149RecursiveArrayIteratorIterator::valid
150RecursiveArrayIteratorIterator::current
151RecursiveArrayIteratorIterator::key
1520=>bcaa
153RecursiveArrayIteratorIterator::next
154RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
155RecursiveArrayIteratorIterator::valid
156RecursiveArrayIteratorIterator::next
157MyRecursiveArrayIterator::valid = false
158RecursiveArrayIteratorIterator::endChildren(2)
159MyRecursiveArrayIterator::valid = false
160RecursiveArrayIteratorIterator::endChildren(1)
161RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
162MyRecursiveArrayIterator::getChildren
163RecursiveArrayIteratorIterator::beginChildren(1)
164RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
165RecursiveArrayIteratorIterator::current
166RecursiveArrayIteratorIterator::key
1670=>ca
168RecursiveArrayIteratorIterator::next
169MyRecursiveArrayIterator::valid = false
170RecursiveArrayIteratorIterator::endChildren(1)
171RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
172RecursiveArrayIteratorIterator::valid
173RecursiveArrayIteratorIterator::current
174RecursiveArrayIteratorIterator::key
1753=>d
176RecursiveArrayIteratorIterator::next
177MyRecursiveArrayIterator::valid = false
178RecursiveArrayIteratorIterator::valid
179MyRecursiveArrayIterator::valid = false
180===DONE===
181