summaryrefslogtreecommitdiff
path: root/ext/spl/tests/iterator_037.phpt
blob: 4b4f14e4d35f2cfe00576a8dbd0f86d18260cc89 (plain)
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
121
122
123
124
125
126
127
128
129
--TEST--
SPL: CachingIterator and __toString
--FILE--
<?php

function test($ar, $flags)
{
	echo "===$flags===\n";
	$it = new CachingIterator($ar, 0);
	try
	{
		$it->setFlags($flags);
	}
	catch (Exception $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($value)
	{
		$this->value = $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: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===5===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===9===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===6===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===10===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===X===
Exception: Unsetting flag CALL_TO_STRING is not possible
Exception: Unsetting flag TOSTRING_USE_INNER is not possible