1--TEST--
2SPL: MultipleIterator
3--FILE--
4<?php
5
6$iter1 = new ArrayIterator(array(1,2,3));
7$iter2 = new ArrayIterator(array(1,2));
8$iter3 = new ArrayIterator(array(new stdClass(),"string",3));
9
10$m = new MultipleIterator();
11
12echo "-- Default flags, no iterators --\n";
13foreach($m as $value) {
14    var_dump($value);
15}
16var_dump($m->current());
17
18$m->attachIterator($iter1);
19$m->attachIterator($iter2);
20$m->attachIterator($iter3);
21
22echo "-- Default flags, MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC --\n";
23
24var_dump($m->getFlags() === (MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC));
25
26foreach($m as $key => $value) {
27    var_dump($key, $value);
28}
29try {
30    $m->current();
31} catch(RuntimeException $e) {
32    echo "RuntimeException thrown: " . $e->getMessage() . "\n";
33}
34try {
35    $m->key();
36} catch(RuntimeException $e) {
37    echo "RuntimeException thrown: " . $e->getMessage() . "\n";
38}
39
40echo "-- Flags = MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC --\n";
41
42$m->setFlags(MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC);
43var_dump($m->getFlags() === (MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC));
44
45foreach($m as $key => $value) {
46    var_dump($key, $value);
47}
48
49echo "-- Default flags, added element --\n";
50
51$m->setFlags(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC);
52
53$iter2[] = 3;
54foreach($m as $key => $value) {
55    var_dump($key, $value);
56}
57
58echo "-- Flags |= MultipleIterator::MIT_KEYS_ASSOC, with iterator associated with NULL --\n";
59
60$m->setFlags(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
61$m->rewind();
62try {
63    $m->current();
64} catch(InvalidArgumentException $e) {
65    echo "InvalidArgumentException thrown: " . $e->getMessage() . "\n";
66}
67
68echo "-- Flags |= MultipleIterator::MIT_KEYS_ASSOC --\n";
69
70$m->attachIterator($iter1, "iter1");
71$m->attachIterator($iter2, "iter2");
72$m->attachIterator($iter3, 3);
73
74foreach($m as $key => $value) {
75    var_dump($key, $value);
76}
77
78echo "-- Associate with invalid value --\n";
79
80try {
81    $m->attachIterator($iter3, new stdClass());
82} catch(TypeError $e) {
83    echo "TypeError thrown: " . $e->getMessage() . "\n";
84}
85
86echo "-- Associate with duplicate value --\n";
87
88try {
89    $m->attachIterator($iter3, "iter1");
90} catch(InvalidArgumentException $e) {
91    echo "InvalidArgumentException thrown: " . $e->getMessage() . "\n";
92}
93
94echo "-- Count, contains, detach, count, contains, iterate --\n";
95
96var_dump($m->countIterators());
97var_dump($m->containsIterator($iter2));
98var_dump($m->detachIterator($iter2));
99var_dump($m->countIterators());
100var_dump($m->containsIterator($iter2));
101foreach($m as $key => $value) {
102    var_dump($key, $value);
103}
104
105?>
106--EXPECTF--
107-- Default flags, no iterators --
108bool(false)
109-- Default flags, MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC --
110bool(true)
111array(3) {
112  [0]=>
113  int(0)
114  [1]=>
115  int(0)
116  [2]=>
117  int(0)
118}
119array(3) {
120  [0]=>
121  int(1)
122  [1]=>
123  int(1)
124  [2]=>
125  object(stdClass)#%d (0) {
126  }
127}
128array(3) {
129  [0]=>
130  int(1)
131  [1]=>
132  int(1)
133  [2]=>
134  int(1)
135}
136array(3) {
137  [0]=>
138  int(2)
139  [1]=>
140  int(2)
141  [2]=>
142  string(6) "string"
143}
144RuntimeException thrown: Called current() with non valid sub iterator
145RuntimeException thrown: Called key() with non valid sub iterator
146-- Flags = MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_NUMERIC --
147bool(true)
148array(3) {
149  [0]=>
150  int(0)
151  [1]=>
152  int(0)
153  [2]=>
154  int(0)
155}
156array(3) {
157  [0]=>
158  int(1)
159  [1]=>
160  int(1)
161  [2]=>
162  object(stdClass)#%d (0) {
163  }
164}
165array(3) {
166  [0]=>
167  int(1)
168  [1]=>
169  int(1)
170  [2]=>
171  int(1)
172}
173array(3) {
174  [0]=>
175  int(2)
176  [1]=>
177  int(2)
178  [2]=>
179  string(6) "string"
180}
181array(3) {
182  [0]=>
183  int(2)
184  [1]=>
185  NULL
186  [2]=>
187  int(2)
188}
189array(3) {
190  [0]=>
191  int(3)
192  [1]=>
193  NULL
194  [2]=>
195  int(3)
196}
197-- Default flags, added element --
198array(3) {
199  [0]=>
200  int(0)
201  [1]=>
202  int(0)
203  [2]=>
204  int(0)
205}
206array(3) {
207  [0]=>
208  int(1)
209  [1]=>
210  int(1)
211  [2]=>
212  object(stdClass)#%d (0) {
213  }
214}
215array(3) {
216  [0]=>
217  int(1)
218  [1]=>
219  int(1)
220  [2]=>
221  int(1)
222}
223array(3) {
224  [0]=>
225  int(2)
226  [1]=>
227  int(2)
228  [2]=>
229  string(6) "string"
230}
231array(3) {
232  [0]=>
233  int(2)
234  [1]=>
235  int(2)
236  [2]=>
237  int(2)
238}
239array(3) {
240  [0]=>
241  int(3)
242  [1]=>
243  int(3)
244  [2]=>
245  int(3)
246}
247-- Flags |= MultipleIterator::MIT_KEYS_ASSOC, with iterator associated with NULL --
248InvalidArgumentException thrown: Sub-Iterator is associated with NULL
249-- Flags |= MultipleIterator::MIT_KEYS_ASSOC --
250array(3) {
251  ["iter1"]=>
252  int(0)
253  ["iter2"]=>
254  int(0)
255  [3]=>
256  int(0)
257}
258array(3) {
259  ["iter1"]=>
260  int(1)
261  ["iter2"]=>
262  int(1)
263  [3]=>
264  object(stdClass)#%d (0) {
265  }
266}
267array(3) {
268  ["iter1"]=>
269  int(1)
270  ["iter2"]=>
271  int(1)
272  [3]=>
273  int(1)
274}
275array(3) {
276  ["iter1"]=>
277  int(2)
278  ["iter2"]=>
279  int(2)
280  [3]=>
281  string(6) "string"
282}
283array(3) {
284  ["iter1"]=>
285  int(2)
286  ["iter2"]=>
287  int(2)
288  [3]=>
289  int(2)
290}
291array(3) {
292  ["iter1"]=>
293  int(3)
294  ["iter2"]=>
295  int(3)
296  [3]=>
297  int(3)
298}
299-- Associate with invalid value --
300TypeError thrown: MultipleIterator::attachIterator(): Argument #2 ($info) must be of type string|int|null, stdClass given
301-- Associate with duplicate value --
302InvalidArgumentException thrown: Key duplication error
303-- Count, contains, detach, count, contains, iterate --
304int(3)
305bool(true)
306NULL
307int(2)
308bool(false)
309array(2) {
310  ["iter1"]=>
311  int(0)
312  [3]=>
313  int(0)
314}
315array(2) {
316  ["iter1"]=>
317  int(1)
318  [3]=>
319  object(stdClass)#%d (0) {
320  }
321}
322array(2) {
323  ["iter1"]=>
324  int(1)
325  [3]=>
326  int(1)
327}
328array(2) {
329  ["iter1"]=>
330  int(2)
331  [3]=>
332  string(6) "string"
333}
334array(2) {
335  ["iter1"]=>
336  int(2)
337  [3]=>
338  int(2)
339}
340array(2) {
341  ["iter1"]=>
342  int(3)
343  [3]=>
344  int(3)
345}
346