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('Passed variable is not an array or object, using empty array instead');
187 if (is_object($data) && ($data instanceof self ||
$data instanceof \ArrayObject
)) {
188 $data = $data->getArrayCopy();
190 if (!is_array($data)) {
191 $data = (array) $data;
194 $storage = $this->storage
;
196 $this->storage
= $data;
202 * Creates a copy of the ArrayObject.
206 public function getArrayCopy()
208 return $this->storage
;
212 * Gets the behavior flags.
216 public function getFlags()
222 * Create a new iterator from an ArrayObject instance
226 public function getIterator()
228 $class = $this->iteratorClass
;
230 return new $class($this->storage
);
234 * Gets the iterator classname for the ArrayObject.
238 public function getIteratorClass()
240 return $this->iteratorClass
;
244 * Sort the entries by key
248 public function ksort()
250 ksort($this->storage
);
254 * Sort an array using a case insensitive "natural order" algorithm
258 public function natcasesort()
260 natcasesort($this->storage
);
264 * Sort entries using a "natural order" algorithm
268 public function natsort()
270 natsort($this->storage
);
274 * Returns whether the requested key exists
279 public function offsetExists($key)
281 return isset($this->storage
[$key]);
285 * Returns the value at the specified key
290 public function &offsetGet($key)
293 if (!$this->offsetExists($key)) {
296 $ret =& $this->storage
[$key];
302 * Sets the value at the specified key to value
305 * @param mixed $value
308 public function offsetSet($key, $value)
310 $this->storage
[$key] = $value;
314 * Unsets the value at the specified key
319 public function offsetUnset($key)
321 if ($this->offsetExists($key)) {
322 unset($this->storage
[$key]);
327 * Serialize an ArrayObject
331 public function serialize()
333 return serialize(get_object_vars($this));
337 * Sets the behavior flags
342 public function setFlags($flags)
344 $this->flag
= $flags;
348 * Sets the iterator classname for the ArrayObject
350 * @param string $class
353 public function setIteratorClass($class)
355 if (class_exists($class)) {
356 $this->iteratorClass
= $class;
361 if (strpos($class, '\\') === 0) {
362 $class = '\\' . $class;
363 if (class_exists($class)) {
364 $this->iteratorClass
= $class;
370 throw new Exception\
InvalidArgumentException('The iterator class does not exist');
374 * Sort the entries with a user-defined comparison function and maintain key association
376 * @param callable $function
379 public function uasort($function)
381 if (is_callable($function)) {
382 uasort($this->storage
, $function);
387 * Sort the entries by keys using a user-defined comparison function
389 * @param callable $function
392 public function uksort($function)
394 if (is_callable($function)) {
395 uksort($this->storage
, $function);
400 * Unserialize an ArrayObject
402 * @param string $data
405 public function unserialize($data)
407 $ar = unserialize($data);
408 $this->protectedProperties
= array_keys(get_object_vars($this));
410 $this->setFlags($ar['flag']);
411 $this->exchangeArray($ar['storage']);
412 $this->setIteratorClass($ar['iteratorClass']);
414 foreach ($ar as $k => $v) {
420 $this->exchangeArray($v);
422 case 'iteratorClass':
423 $this->setIteratorClass($v);
425 case 'protectedProperties':
428 $this->__set($k, $v);