xref: /PHP-5.6/ext/spl/tests/iterator_022.phpt (revision 610c7fbe)
1--TEST--
2SPL: RecursiveIteratorIterator and callHasChildren/callGetChildren
3--FILE--
4<?php
5
6class MyRecursiveArrayIterator extends RecursiveArrayIterator
7{
8	function getChildren()
9	{
10		echo __METHOD__ . "\n";
11		return $this->current();
12	}
13
14	function valid()
15	{
16		if (!parent::valid())
17		{
18			echo __METHOD__ . " = false\n";
19			return false;
20		}
21		else
22		{
23			return true;
24		}
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 callGetChildren()
93	{
94		if ($this->over == 2)
95		{
96			echo __METHOD__ . "(skip)\n";
97			return NULL;
98		}
99		echo __METHOD__ . "(ok:{$this->over})\n";
100		return new MyRecursiveArrayIterator($this->current());
101	}
102
103	function beginChildren()
104	{
105		echo __METHOD__ . "(".$this->getDepth().")\n";
106	}
107
108	function endChildren()
109	{
110		echo __METHOD__ . "(".$this->getDepth().")\n";
111	}
112}
113
114try
115{
116	foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v)
117	{
118		if (is_array($v)) $v = join('',$v);
119		echo "$k=>$v\n";
120	}
121}
122catch(UnexpectedValueException $e)
123{
124	echo $e->getMessage() . "\n";
125}
126
127?>
128===DONE===
129<?php exit(0); ?>
130--EXPECT--
131RecursiveArrayIteratorIterator::rewind
132RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
133RecursiveArrayIteratorIterator::valid
134RecursiveArrayIteratorIterator::current
135RecursiveArrayIteratorIterator::key
1360=>a
137RecursiveArrayIteratorIterator::next
138RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
139RecursiveArrayIteratorIterator::callGetChildren(ok:0)
140RecursiveArrayIteratorIterator::current
141RecursiveArrayIteratorIterator::beginChildren(1)
142RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
143RecursiveArrayIteratorIterator::valid
144RecursiveArrayIteratorIterator::current
145RecursiveArrayIteratorIterator::key
1460=>ba
147RecursiveArrayIteratorIterator::next
148RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
149RecursiveArrayIteratorIterator::callGetChildren(ok:0)
150RecursiveArrayIteratorIterator::current
151RecursiveArrayIteratorIterator::beginChildren(2)
152RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
153RecursiveArrayIteratorIterator::valid
154RecursiveArrayIteratorIterator::current
155RecursiveArrayIteratorIterator::key
1560=>bba
157RecursiveArrayIteratorIterator::next
158RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
159RecursiveArrayIteratorIterator::valid
160RecursiveArrayIteratorIterator::current
161RecursiveArrayIteratorIterator::key
1621=>bbb
163RecursiveArrayIteratorIterator::next
164MyRecursiveArrayIterator::valid = false
165RecursiveArrayIteratorIterator::endChildren(2)
166RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
167RecursiveArrayIteratorIterator::callGetChildren(ok:0)
168RecursiveArrayIteratorIterator::current
169RecursiveArrayIteratorIterator::beginChildren(2)
170RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
171RecursiveArrayIteratorIterator::valid
172RecursiveArrayIteratorIterator::current
173RecursiveArrayIteratorIterator::key
1740=>bcaa
175RecursiveArrayIteratorIterator::next
176RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
177RecursiveArrayIteratorIterator::valid
178RecursiveArrayIteratorIterator::next
179MyRecursiveArrayIterator::valid = false
180RecursiveArrayIteratorIterator::endChildren(2)
181MyRecursiveArrayIterator::valid = false
182RecursiveArrayIteratorIterator::endChildren(1)
183RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
184RecursiveArrayIteratorIterator::callGetChildren(skip)
185Objects returned by RecursiveIterator::getChildren() must implement RecursiveIterator
186===DONE===
187