3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib;
14 use IteratorAggregate;
18 * Custom framework ArrayObject implementation
20 * Extends version-specific "abstract" implementation.
22 class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
25 * Properties of the object have their normal functionality
26 * when accessed as list (var_dump, foreach, etc.).
28 const STD_PROP_LIST = 1;
31 * Entries can be accessed as properties (read and write).
33 const ARRAY_AS_PROPS = 2;
48 protected $iteratorClass;
53 protected $protectedProperties;
60 * @param string $iteratorClass
62 public function __construct($input = [], $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator')
64 $this->setFlags($flags);
65 $this->storage = $input;
66 $this->setIteratorClass($iteratorClass);
67 $this->protectedProperties = array_keys(get_object_vars($this));
71 * Returns whether the requested key exists
76 public function __isset($key)
78 if ($this->flag == self::ARRAY_AS_PROPS) {
79 return $this->offsetExists($key);
81 if (in_array($key, $this->protectedProperties)) {
82 throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
85 return isset($this->$key);
89 * Sets the value at the specified key to value
95 public function __set($key, $value)
97 if ($this->flag == self::ARRAY_AS_PROPS) {
98 return $this->offsetSet($key, $value);
100 if (in_array($key, $this->protectedProperties)) {
101 throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
103 $this->$key = $value;
107 * Unsets the value at the specified key
112 public function __unset($key)
114 if ($this->flag == self::ARRAY_AS_PROPS) {
115 return $this->offsetUnset($key);
117 if (in_array($key, $this->protectedProperties)) {
118 throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
124 * Returns the value at the specified key by reference
129 public function &__get($key)
132 if ($this->flag == self::ARRAY_AS_PROPS) {
133 $ret =& $this->offsetGet($key);
137 if (in_array($key, $this->protectedProperties)) {
138 throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
147 * @param mixed $value
150 public function append($value)
152 $this->storage[] = $value;
156 * Sort the entries by value
160 public function asort()
162 asort($this->storage);
166 * Get the number of public properties in the ArrayObject
170 public function count()
172 return count($this->storage);
176 * Exchange the array for another one.
178 * @param array|ArrayObject $data
181 public function exchangeArray($data)
183 if (! is_array($data) && ! is_object($data)) {
184 throw new Exception\InvalidArgumentException(
185 'Passed variable is not an array or object, using empty array instead'
189 if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
190 $data = $data->getArrayCopy();
192 if (! is_array($data)) {
193 $data = (array) $data;
196 $storage = $this->storage;
198 $this->storage = $data;
204 * Creates a copy of the ArrayObject.
208 public function getArrayCopy()
210 return $this->storage;
214 * Gets the behavior flags.
218 public function getFlags()
224 * Create a new iterator from an ArrayObject instance
228 public function getIterator()
230 $class = $this->iteratorClass;
232 return new $class($this->storage);
236 * Gets the iterator classname for the ArrayObject.
240 public function getIteratorClass()
242 return $this->iteratorClass;
246 * Sort the entries by key
250 public function ksort()
252 ksort($this->storage);
256 * Sort an array using a case insensitive "natural order" algorithm
260 public function natcasesort()
262 natcasesort($this->storage);
266 * Sort entries using a "natural order" algorithm
270 public function natsort()
272 natsort($this->storage);
276 * Returns whether the requested key exists
281 public function offsetExists($key)
283 return isset($this->storage[$key]);
287 * Returns the value at the specified key
292 public function &offsetGet($key)
295 if (! $this->offsetExists($key)) {
298 $ret =& $this->storage[$key];
304 * Sets the value at the specified key to value
307 * @param mixed $value
310 public function offsetSet($key, $value)
312 $this->storage[$key] = $value;
316 * Unsets the value at the specified key
321 public function offsetUnset($key)
323 if ($this->offsetExists($key)) {
324 unset($this->storage[$key]);
329 * Serialize an ArrayObject
333 public function serialize()
335 return serialize(get_object_vars($this));
339 * Sets the behavior flags
344 public function setFlags($flags)
346 $this->flag = $flags;
350 * Sets the iterator classname for the ArrayObject
352 * @param string $class
355 public function setIteratorClass($class)
357 if (class_exists($class)) {
358 $this->iteratorClass = $class;
363 if (strpos($class, '\\') === 0) {
364 $class = '\\' . $class;
365 if (class_exists($class)) {
366 $this->iteratorClass = $class;
372 throw new Exception\InvalidArgumentException('The iterator class does not exist');
376 * Sort the entries with a user-defined comparison function and maintain key association
378 * @param callable $function
381 public function uasort($function)
383 if (is_callable($function)) {
384 uasort($this->storage, $function);
389 * Sort the entries by keys using a user-defined comparison function
391 * @param callable $function
394 public function uksort($function)
396 if (is_callable($function)) {
397 uksort($this->storage, $function);
402 * Unserialize an ArrayObject
404 * @param string $data
407 public function unserialize($data)
409 $ar = unserialize($data);
410 $this->protectedProperties = array_keys(get_object_vars($this));
412 $this->setFlags($ar['flag']);
413 $this->exchangeArray($ar['storage']);
414 $this->setIteratorClass($ar['iteratorClass']);
416 foreach ($ar as $k => $v) {
422 $this->exchangeArray($v);
424 case 'iteratorClass':
425 $this->setIteratorClass($v);
427 case 'protectedProperties':
430 $this->__set($k, $v);