xref: /PHP-8.4/ext/spl/tests/iterator_028.phpt (revision 68195bd4)
1--TEST--
2SPL: RecursiveIteratorIterator and setMaxDepth()
3--FILE--
4<?php
5
6$ar = array(1, 2, array(31, 32, array(331, array(3321, array(33221)))), 4);
7
8$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($ar));
9
10echo "===?===\n";
11var_dump($it->getMaxDepth());
12foreach($it as $v) echo $it->getDepth() . ": $v\n";
13
14echo "===2===\n";
15$it->setMaxDepth(2);
16var_dump($it->getMaxDepth());
17foreach($it as $v) echo $it->getDepth() . ": $v\n";
18
19echo "===X===\n";
20$it->setMaxDepth();
21var_dump($it->getMaxDepth());
22foreach($it as $v) echo $it->getDepth() . ": $v\n";
23
24echo "===3===\n";
25$it->setMaxDepth(3);
26var_dump($it->getMaxDepth());
27foreach($it as $v) echo $it->getDepth() . ": $v\n";
28
29echo "===5===\n";
30$it->setMaxDepth(5);
31var_dump($it->getMaxDepth());
32foreach($it as $v) echo $it->getDepth() . ": $v\n";
33
34echo "===0===\n";
35$it->setMaxDepth(0);
36var_dump($it->getMaxDepth());
37foreach($it as $v) echo $it->getDepth() . ": $v\n";
38
39echo "===-1===\n";
40$it->setMaxDepth(-1);
41var_dump($it->getMaxDepth());
42$it->setMaxDepth(4);
43try {
44    $it->setMaxDepth(-2);
45} catch(\ValueError $e) {
46    echo $e->getMessage() . \PHP_EOL;
47}
48var_dump($it->getMaxDepth());
49?>
50--EXPECT--
51===?===
52bool(false)
530: 1
540: 2
551: 31
561: 32
572: 331
583: 3321
594: 33221
600: 4
61===2===
62int(2)
630: 1
640: 2
651: 31
661: 32
672: 331
680: 4
69===X===
70bool(false)
710: 1
720: 2
731: 31
741: 32
752: 331
763: 3321
774: 33221
780: 4
79===3===
80int(3)
810: 1
820: 2
831: 31
841: 32
852: 331
863: 3321
870: 4
88===5===
89int(5)
900: 1
910: 2
921: 31
931: 32
942: 331
953: 3321
964: 33221
970: 4
98===0===
99int(0)
1000: 1
1010: 2
1020: 4
103===-1===
104bool(false)
105RecursiveIteratorIterator::setMaxDepth(): Argument #1 ($maxDepth) must be greater than or equal to -1
106int(4)
107