Overview

Packages

  • Support

Classes

  • Horde_Support_Array
  • Horde_Support_Backtrace
  • Horde_Support_CombineStream
  • Horde_Support_ConsistentHash
  • Horde_Support_Guid
  • Horde_Support_Inflector
  • Horde_Support_Memory
  • Horde_Support_Numerizer
  • Horde_Support_Numerizer_Locale_Base
  • Horde_Support_Numerizer_Locale_De
  • Horde_Support_Numerizer_Locale_Pt
  • Horde_Support_Randomid
  • Horde_Support_Stack
  • Horde_Support_StringStream
  • Horde_Support_Stub
  • Horde_Support_Timer
  • Horde_Support_Uuid
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * @todo - Incorporate stuff from Horde_Array?
  6:  *       - http://docs.python.org/lib/typesmapping.html
  7:  *
  8:  * @category   Horde
  9:  * @package    Support
 10:  * @license    http://www.horde.org/licenses/bsd
 11:  */
 12: class Horde_Support_Array implements ArrayAccess, Countable, Iterator
 13: {
 14:     /**
 15:      * Array variables
 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:      * Gets the value at $offset. If no value exists at that offset, or the
 37:      * value $offset is NULL, then $default is set as the value of $offset.
 38:      *
 39:      * @param string $offset   Offset to retrieve and set if unset
 40:      * @param string $default  Default value if $offset does not exist
 41:      *
 42:      * @return mixed Value at $offset or $default
 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:      * Gets the value at $offset and deletes it from the array. If no value
 55:      * exists at $offset, or the value at $offset is null, then $default
 56:      * will be returned.
 57:      *
 58:      * @param string $offset   Offset to pop
 59:      * @param string $default  Default value
 60:      *
 61:      * @return mixed Value at $offset or $default
 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:      * Update the array with the key/value pairs from $array
 72:      *
 73:      * @param array $array Key/value pairs to set/change in the array.
 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:      * Get the keys in the array
 88:      *
 89:      * @return array
 90:      */
 91:     public function getKeys()
 92:     {
 93:         return array_keys($this->_array);
 94:     }
 95: 
 96:     /**
 97:      * Get the values in the array
 98:      *
 99:      * @return array
100:      */
101:     public function getValues()
102:     {
103:         return array_values($this->_array);
104:     }
105: 
106:     /**
107:      * Clear out the array
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:      * Checks the existance of $key in this array
130:      */
131:     public function __isset($key)
132:     {
133:         return array_key_exists($key, $this->_array);
134:     }
135: 
136:     /**
137:      * Removes $key from this array
138:      */
139:     public function __unset($key)
140:     {
141:         unset($this->_array[$key]);
142:     }
143: 
144:     /**
145:      * Count the number of elements
146:      *
147:      * @return integer
148:      */
149:     public function count()
150:     {
151:         return count($this->_array);
152:     }
153: 
154:     /**
155:      * Gets the current value of this array's Iterator
156:      */
157:     public function current()
158:     {
159:         return current($this->_array);
160:     }
161: 
162:     /**
163:      * Advances this array's Iterator to the next value
164:      */
165:     public function next()
166:     {
167:         return next($this->_array);
168:     }
169: 
170:     /**
171:      * Returns the current key of this array's Iterator
172:      */
173:     public function key()
174:     {
175:         return key($this->_array);
176:     }
177: 
178:     /**
179:      * Checks if this array's Iterator is in a valid position
180:      */
181:     public function valid()
182:     {
183:         return $this->current() !== false;
184:     }
185: 
186:     /**
187:      * Rewinds this array's Iterator
188:      */
189:     public function rewind()
190:     {
191:         reset($this->_array);
192:     }
193: 
194:     /**
195:      * Gets the value of $offset in this array
196:      *
197:      * @see __get()
198:      */
199:     public function offsetGet($offset)
200:     {
201:         return $this->__get($offset);
202:     }
203: 
204:     /**
205:      * Sets the value of $offset to $value
206:      *
207:      * @see __set()
208:      */
209:     public function offsetSet($offset, $value)
210:     {
211:         return $this->__set($offset, $value);
212:     }
213: 
214:     /**
215:      * Checks the existence of $offset in this array
216:      *
217:      * @see __isset()
218:      */
219:     public function offsetExists($offset)
220:     {
221:         return $this->__isset($offset);
222:     }
223: 
224:     /**
225:      * Removes $offset from this array
226:      *
227:      * @see __unset()
228:      */
229:     public function offsetUnset($offset)
230:     {
231:         return $this->__unset($offset);
232:     }
233: 
234: }
235: 
API documentation generated by ApiGen