File: iterator_037.phpt

package info (click to toggle)
php8.4 8.4.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 211,276 kB
  • sloc: ansic: 1,176,142; php: 35,419; sh: 11,964; cpp: 7,208; pascal: 4,951; javascript: 3,091; asm: 2,817; yacc: 2,411; makefile: 696; xml: 446; python: 301; awk: 148
file content (120 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--TEST--
SPL: CachingIterator and __toString
--FILE--
<?php

function test($ar, $flags)
{
    echo "===$flags===\n";
    $it = new CachingIterator($ar, 0);
    try {
        $it->setFlags($flags);
    } catch (\ValueError $e) {
        echo 'Exception: ' . $e->getMessage() . "\n";
        var_dump($it->getFlags());
        return;
    }
    var_dump($it->getFlags());
    try {
        foreach($it as $v)
        {
            var_dump((string)$it);
        }
    } catch (Exception $e) {
        echo 'Exception: ' . $e->getMessage() . "\n";
    }
}

class MyItem
{
    function __construct(public $value) {}

    function __toString()
    {
        return (string)$this->value;
    }
}

class MyArrayIterator extends ArrayIterator
{
    function __toString()
    {
        return $this->key() . ':' . $this->current();
    }
}

$ar = new MyArrayIterator(array(1, 2, 3));

test($ar, CachingIterator::CALL_TOSTRING);
test($ar, CachingIterator::TOSTRING_USE_KEY);
test($ar, CachingIterator::TOSTRING_USE_CURRENT);

$ar = new MyArrayIterator(array(new MyItem(1), new MyItem(2), new MyItem(3)));

test($ar, CachingIterator::TOSTRING_USE_INNER);
test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_KEY);
test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_CURRENT);
test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_INNER);
test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_CURRENT);
test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_INNER);

echo "===X===\n";
try
{
    $it = new CachingIterator($ar, CachingIterator::CALL_TOSTRING);
    $it->setFlags(0);
}
catch (Exception $e)
{
    echo 'Exception: ' . $e->getMessage() . "\n";
}
try
{
    $it = new CachingIterator($ar, CachingIterator::TOSTRING_USE_INNER);
    $it->setFlags(0);
}
catch (Exception $e)
{
    echo 'Exception: ' . $e->getMessage() . "\n";
}

?>
--EXPECT--
===1===
int(1)
string(1) "1"
string(1) "2"
string(1) "3"
===2===
int(2)
string(1) "0"
string(1) "1"
string(1) "2"
===4===
int(4)
string(1) "1"
string(1) "2"
string(1) "3"
===8===
int(8)
string(3) "0:1"
string(3) "1:2"
string(3) "2:3"
===3===
Exception: 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
int(0)
===5===
Exception: 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
int(0)
===9===
Exception: 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
int(0)
===6===
Exception: 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
int(0)
===10===
Exception: 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
int(0)
===X===
Exception: Unsetting flag CALL_TO_STRING is not possible
Exception: Unsetting flag TOSTRING_USE_INNER is not possible