1--TEST--
2ZE2 ArrayAccess and ArrayProxyAccess, ArrayProxy
3--FILE--
4<?php
5
6// NOTE: This will become part of SPL
7
8interface ArrayProxyAccess extends ArrayAccess
9{
10	function proxyGet($element);
11	function proxySet($element, $index, $value);
12	function proxyUnset($element, $index);
13}
14
15class ArrayProxy implements ArrayAccess
16{
17	private $object;
18	private $element;
19
20	function __construct(ArrayProxyAccess $object, $element)
21	{
22		echo __METHOD__ . "($element)\n";
23		if (!$object->offsetExists($element))
24		{
25			$object[$element] = array();
26		}
27		$this->object = $object;
28		$this->element = $element;
29	}
30
31	function offsetExists($index) {
32		echo __METHOD__ . "($this->element, $index)\n";
33		return array_key_exists($index, $this->object->proxyGet($this->element));
34	}
35
36	function offsetGet($index) {
37		echo __METHOD__ . "($this->element, $index)\n";
38		$tmp = $this->object->proxyGet($this->element);
39		return isset($tmp[$index]) ? $tmp[$index] : NULL;
40	}
41
42	function offsetSet($index, $value) {
43		echo __METHOD__ . "($this->element, $index, $value)\n";
44		$this->object->proxySet($this->element, $index, $value);
45	}
46
47	function offsetUnset($index) {
48		echo __METHOD__ . "($this->element, $index)\n";
49		$this->object->proxyUnset($this->element, $index);
50	}
51}
52
53class Peoples implements ArrayProxyAccess
54{
55	public $person;
56
57	function __construct()
58	{
59		$this->person = array(array('name'=>'Foo'));
60	}
61
62	function offsetExists($index)
63	{
64		return array_key_exists($index, $this->person);
65	}
66
67	function offsetGet($index)
68	{
69		return new ArrayProxy($this, $index);
70	}
71
72	function offsetSet($index, $value)
73	{
74		$this->person[$index] = $value;
75	}
76
77	function offsetUnset($index)
78	{
79		unset($this->person[$index]);
80	}
81
82	function proxyGet($element)
83	{
84		return $this->person[$element];
85	}
86
87	function proxySet($element, $index, $value)
88	{
89		$this->person[$element][$index] = $value;
90	}
91
92	function proxyUnset($element, $index)
93	{
94		unset($this->person[$element][$index]);
95	}
96}
97
98$people = new Peoples;
99
100var_dump($people->person[0]['name']);
101$people->person[0]['name'] = $people->person[0]['name'] . 'Bar';
102var_dump($people->person[0]['name']);
103$people->person[0]['name'] .= 'Baz';
104var_dump($people->person[0]['name']);
105
106echo "===ArrayOverloading===\n";
107
108$people = new Peoples;
109
110var_dump($people[0]);
111var_dump($people[0]['name']);
112$people[0]['name'] = 'FooBar';
113var_dump($people[0]['name']);
114$people[0]['name'] = $people->person[0]['name'] . 'Bar';
115var_dump($people[0]['name']);
116$people[0]['name'] .= 'Baz';
117var_dump($people[0]['name']);
118unset($people[0]['name']);
119var_dump($people[0]);
120var_dump($people[0]['name']);
121$people[0]['name'] = 'BlaBla';
122var_dump($people[0]['name']);
123
124?>
125===DONE===
126--EXPECTF--
127string(3) "Foo"
128string(6) "FooBar"
129string(9) "FooBarBaz"
130===ArrayOverloading===
131ArrayProxy::__construct(0)
132object(ArrayProxy)#%d (2) {
133  ["object":"ArrayProxy":private]=>
134  object(Peoples)#%d (1) {
135    ["person"]=>
136    array(1) {
137      [0]=>
138      array(1) {
139        ["name"]=>
140        string(3) "Foo"
141      }
142    }
143  }
144  ["element":"ArrayProxy":private]=>
145  int(0)
146}
147ArrayProxy::__construct(0)
148ArrayProxy::offsetGet(0, name)
149string(3) "Foo"
150ArrayProxy::__construct(0)
151ArrayProxy::offsetSet(0, name, FooBar)
152ArrayProxy::__construct(0)
153ArrayProxy::offsetGet(0, name)
154string(6) "FooBar"
155ArrayProxy::__construct(0)
156ArrayProxy::offsetSet(0, name, FooBarBar)
157ArrayProxy::__construct(0)
158ArrayProxy::offsetGet(0, name)
159string(9) "FooBarBar"
160ArrayProxy::__construct(0)
161ArrayProxy::offsetGet(0, name)
162ArrayProxy::offsetSet(0, name, FooBarBarBaz)
163ArrayProxy::__construct(0)
164ArrayProxy::offsetGet(0, name)
165string(12) "FooBarBarBaz"
166ArrayProxy::__construct(0)
167ArrayProxy::offsetUnset(0, name)
168ArrayProxy::__construct(0)
169object(ArrayProxy)#%d (2) {
170  ["object":"ArrayProxy":private]=>
171  object(Peoples)#%d (1) {
172    ["person"]=>
173    array(1) {
174      [0]=>
175      array(0) {
176      }
177    }
178  }
179  ["element":"ArrayProxy":private]=>
180  int(0)
181}
182ArrayProxy::__construct(0)
183ArrayProxy::offsetGet(0, name)
184NULL
185ArrayProxy::__construct(0)
186ArrayProxy::offsetSet(0, name, BlaBla)
187ArrayProxy::__construct(0)
188ArrayProxy::offsetGet(0, name)
189string(6) "BlaBla"
190===DONE===
191