1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Horde_Support_Array implements ArrayAccess, Countable, Iterator
13: {
14: 15: 16:
17: protected $_array = array();
18:
19: 20:
21: public function __construct($vars = array())
22: {
23: if (is_array($vars)) {
24: $this->update($vars);
25: }
26: }
27:
28: 29:
30: public function get($key, $default = null)
31: {
32: return isset($this->_array[$key]) ? $this->_array[$key] : $default;
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42: 43:
44: public function getOrSet($offset, $default = null)
45: {
46: $value = $this->offsetGet($offset);
47: if (is_null($value)) {
48: $this->offsetSet($offset, $value = $default);
49: }
50: return $value;
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public function pop($offset, $default = null)
64: {
65: $value = $this->offsetGet($offset);
66: $this->offsetUnset($offset);
67: return isset($value) ? $value : $default;
68: }
69:
70: 71: 72: 73: 74:
75: public function update($array)
76: {
77: if (!is_array($array) && !$array instanceof Traversable) {
78: throw new InvalidArgumentException('expected array or traversable, got ' . gettype($array));
79: }
80:
81: foreach ($array as $key => $val) {
82: $this->offsetSet($key, $val);
83: }
84: }
85:
86: 87: 88: 89: 90:
91: public function getKeys()
92: {
93: return array_keys($this->_array);
94: }
95:
96: 97: 98: 99: 100:
101: public function getValues()
102: {
103: return array_values($this->_array);
104: }
105:
106: 107: 108:
109: public function clear()
110: {
111: $this->_array = array();
112: }
113:
114: 115:
116: public function __get($key)
117: {
118: return $this->get($key);
119: }
120:
121: 122:
123: public function __set($key, $value)
124: {
125: $this->_array[$key] = $value;
126: }
127:
128: 129: 130:
131: public function __isset($key)
132: {
133: return array_key_exists($key, $this->_array);
134: }
135:
136: 137: 138:
139: public function __unset($key)
140: {
141: unset($this->_array[$key]);
142: }
143:
144: 145: 146: 147: 148:
149: public function count()
150: {
151: return count($this->_array);
152: }
153:
154: 155: 156:
157: public function current()
158: {
159: return current($this->_array);
160: }
161:
162: 163: 164:
165: public function next()
166: {
167: return next($this->_array);
168: }
169:
170: 171: 172:
173: public function key()
174: {
175: return key($this->_array);
176: }
177:
178: 179: 180:
181: public function valid()
182: {
183: return $this->current() !== false;
184: }
185:
186: 187: 188:
189: public function rewind()
190: {
191: reset($this->_array);
192: }
193:
194: 195: 196: 197: 198:
199: public function offsetGet($offset)
200: {
201: return $this->__get($offset);
202: }
203:
204: 205: 206: 207: 208:
209: public function offsetSet($offset, $value)
210: {
211: return $this->__set($offset, $value);
212: }
213:
214: 215: 216: 217: 218:
219: public function offsetExists($offset)
220: {
221: return $this->__isset($offset);
222: }
223:
224: 225: 226: 227: 228:
229: public function offsetUnset($offset)
230: {
231: return $this->__unset($offset);
232: }
233:
234: }
235: