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     public function offsetGet($offset)
28     {
29         #
30         # workdaround for &offsetGet for PHP < 5.3.4
31         #
32 
33         $v = &$this->values[$offset];
34 
35         return $v;
36     }
37 
38     public function offsetSet($offset, $value)
39     {
40         $this->values[$offset] = $value;
41     }
42 
43     public function offsetUnset($offset)
44     {
45         unset($this->values[$offset]);
46     }
47 
48     /*
49      * /ArrayAccss
50      */
51 
52     public function getIterator()
53     {
54         return new \ArrayIterator($this->values);
55     }
56 
57     public function push()
58     {
59         $this->depth++;
60         array_push($this->values_stack, $this->values);
61     }
62 
63     public function pop()
64     {
65         $this->depth--;
66         $this->values = array_pop($this->values_stack);
67     }
68 
69     public function keys()
70     {
71         return array_keys($this->values);
72     }
73 
74     public function values()
75     {
76         return array_values($this->values);
77     }
78 }