xref: /PHP-8.3/ext/spl/tests/iterator_037.phpt (revision 902d6439)
1--TEST--
2SPL: CachingIterator and __toString
3--FILE--
4<?php
5
6function test($ar, $flags)
7{
8    echo "===$flags===\n";
9    $it = new CachingIterator($ar, 0);
10    try {
11        $it->setFlags($flags);
12    } catch (\ValueError $e) {
13        echo 'Exception: ' . $e->getMessage() . "\n";
14        var_dump($it->getFlags());
15        return;
16    }
17    var_dump($it->getFlags());
18    try {
19        foreach($it as $v)
20        {
21            var_dump((string)$it);
22        }
23    } catch (Exception $e) {
24        echo 'Exception: ' . $e->getMessage() . "\n";
25    }
26}
27
28class MyItem
29{
30    function __construct(public $value) {}
31
32    function __toString()
33    {
34        return (string)$this->value;
35    }
36}
37
38class MyArrayIterator extends ArrayIterator
39{
40    function __toString()
41    {
42        return $this->key() . ':' . $this->current();
43    }
44}
45
46$ar = new MyArrayIterator(array(1, 2, 3));
47
48test($ar, CachingIterator::CALL_TOSTRING);
49test($ar, CachingIterator::TOSTRING_USE_KEY);
50test($ar, CachingIterator::TOSTRING_USE_CURRENT);
51
52$ar = new MyArrayIterator(array(new MyItem(1), new MyItem(2), new MyItem(3)));
53
54test($ar, CachingIterator::TOSTRING_USE_INNER);
55test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_KEY);
56test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_CURRENT);
57test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_INNER);
58test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_CURRENT);
59test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_INNER);
60
61echo "===X===\n";
62try
63{
64    $it = new CachingIterator($ar, CachingIterator::CALL_TOSTRING);
65    $it->setFlags(0);
66}
67catch (Exception $e)
68{
69    echo 'Exception: ' . $e->getMessage() . "\n";
70}
71try
72{
73    $it = new CachingIterator($ar, CachingIterator::TOSTRING_USE_INNER);
74    $it->setFlags(0);
75}
76catch (Exception $e)
77{
78    echo 'Exception: ' . $e->getMessage() . "\n";
79}
80
81?>
82--EXPECT--
83===1===
84int(1)
85string(1) "1"
86string(1) "2"
87string(1) "3"
88===2===
89int(2)
90string(1) "0"
91string(1) "1"
92string(1) "2"
93===4===
94int(4)
95string(1) "1"
96string(1) "2"
97string(1) "3"
98===8===
99int(8)
100string(3) "0:1"
101string(3) "1:2"
102string(3) "2:3"
103===3===
104Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
105int(0)
106===5===
107Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
108int(0)
109===9===
110Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
111int(0)
112===6===
113Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
114int(0)
115===10===
116Exception: CachingIterator::setFlags(): Argument #1 ($flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER
117int(0)
118===X===
119Exception: Unsetting flag CALL_TO_STRING is not possible
120Exception: Unsetting flag TOSTRING_USE_INNER is not possible
121