xref: /PHP-8.1/ext/spl/tests/iterator_037.phpt (revision 063fdd94)
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($value)
31    {
32        $this->value = $value;
33    }
34
35    function __toString()
36    {
37        return (string)$this->value;
38    }
39}
40
41class MyArrayIterator extends ArrayIterator
42{
43    function __toString()
44    {
45        return $this->key() . ':' . $this->current();
46    }
47}
48
49$ar = new MyArrayIterator(array(1, 2, 3));
50
51test($ar, CachingIterator::CALL_TOSTRING);
52test($ar, CachingIterator::TOSTRING_USE_KEY);
53test($ar, CachingIterator::TOSTRING_USE_CURRENT);
54
55$ar = new MyArrayIterator(array(new MyItem(1), new MyItem(2), new MyItem(3)));
56
57test($ar, CachingIterator::TOSTRING_USE_INNER);
58test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_KEY);
59test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_CURRENT);
60test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_INNER);
61test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_CURRENT);
62test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_INNER);
63
64echo "===X===\n";
65try
66{
67    $it = new CachingIterator($ar, CachingIterator::CALL_TOSTRING);
68    $it->setFlags(0);
69}
70catch (Exception $e)
71{
72    echo 'Exception: ' . $e->getMessage() . "\n";
73}
74try
75{
76    $it = new CachingIterator($ar, CachingIterator::TOSTRING_USE_INNER);
77    $it->setFlags(0);
78}
79catch (Exception $e)
80{
81    echo 'Exception: ' . $e->getMessage() . "\n";
82}
83
84?>
85--EXPECT--
86===1===
87int(1)
88string(1) "1"
89string(1) "2"
90string(1) "3"
91===2===
92int(2)
93string(1) "0"
94string(1) "1"
95string(1) "2"
96===4===
97int(4)
98string(1) "1"
99string(1) "2"
100string(1) "3"
101===8===
102int(8)
103string(3) "0:1"
104string(3) "1:2"
105string(3) "2:3"
106===3===
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===5===
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===9===
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===6===
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===10===
119Exception: 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
120int(0)
121===X===
122Exception: Unsetting flag CALL_TO_STRING is not possible
123Exception: Unsetting flag TOSTRING_USE_INNER is not possible
124