1 <?php
2
3 namespace BlueTihi;
4
5 class Context implements \ArrayAccess, \IteratorAggregate
6 {
7 protected $values = array();
8 protected $values_stack = array();
9 protected $depth = 0;
10 protected $this_arg;
11
12 public function __construct(array $values, $this_arg=null)
13 {
14 $this->values = $values;
15 $this->this_arg = $this_arg;
16 }
17
18 /*
19 * ArrayAccess
20 */
21
22 public function offsetExists($offset)
23 {
24 return isset($this->values[$offset]);
25 }
26
27 /*
28 public function offsetGet($offset)
29 {
30 #
31 # workdaround for &offsetGet for PHP < 5.3.4
32 #
33
34 $v = &$this->values[$offset];
35
36 return $v;
37 }
38 */
39
40 public function &offsetGet($offset)
41 {
42 return $this->values[$offset];
43 }
44
45 public function offsetSet($offset, $value)
46 {
47 $this->values[$offset] = $value;
48 }
49
50 public function offsetUnset($offset)
51 {
52 unset($this->values[$offset]);
53 }
54
55 /*
56 * /ArrayAccss
57 */
58
59 public function getIterator()
60 {
61 return new \ArrayIterator($this->values);
62 }
63
64 public function push()
65 {
66 $this->depth++;
67 array_push($this->values_stack, $this->values);
68 }
69
70 public function pop()
71 {
72 $this->depth--;
73 $this->values = array_pop($this->values_stack);
74 }
75
76 public function keys()
77 {
78 return array_keys($this->values);
79 }
80
81 public function values()
82 {
83 return array_values($this->values);
84 }
85 }