xref: /PHP-8.4/ext/spl/tests/iterator_002.phpt (revision c6357b80)
1--TEST--
2SPL: Iterator using getInnerIterator
3--FILE--
4<?php
5
6class RecursiceArrayIterator extends ArrayIterator implements RecursiveIterator
7{
8    function hasChildren(): bool
9    {
10        return is_array($this->current());
11    }
12
13    function getChildren(): RecursiceArrayIterator
14    {
15        return new RecursiceArrayIterator($this->current());
16    }
17}
18
19class CrashIterator extends FilterIterator implements RecursiveIterator
20{
21    function accept(): bool
22    {
23        return true;
24    }
25
26    function hasChildren(): bool
27    {
28        return $this->getInnerIterator()->hasChildren();
29    }
30
31    function getChildren(): RecursiceArrayIterator
32    {
33        return new RecursiceArrayIterator($this->getInnerIterator()->current());
34    }
35}
36
37$array = array(1, 2 => array(21, 22 => array(221, 222), 23 => array(231)), 3);
38
39$dir = new RecursiveIteratorIterator(new CrashIterator(new RecursiceArrayIterator($array)), RecursiveIteratorIterator::LEAVES_ONLY);
40
41foreach ($dir as $file) {
42    print "$file\n";
43}
44
45?>
46--EXPECT--
471
4821
49221
50222
51231
523
53