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