diff --git a/composer.json b/composer.json index 98a82ab7b..c75458a9f 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,10 @@ "autoload": { "psr-4": { "Zend\\Stdlib\\": "src/" - } + }, + "files": [ + "compatibility/autoload.php" + ] }, "require": { "php": ">=5.3.3" diff --git a/src/ArrayObject.php b/src/ArrayObject.php index 987c97670..98f3a09bd 100644 --- a/src/ArrayObject.php +++ b/src/ArrayObject.php @@ -9,430 +9,411 @@ namespace Zend\Stdlib; -use ArrayObject as PhpArrayObject; use IteratorAggregate; use ArrayAccess; use Serializable; use Countable; -// use our own version of arrayobject to handle references -if (version_compare(PHP_VERSION, '5.3.3') > 0) { +/** + * ArrayObject + * + * This ArrayObject is a rewrite of the implementation to fix + * issues with php's implementation of ArrayObject where you + * are unable to unset multi-dimensional arrays because you + * need to fetch the properties / lists as references. + */ +class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable +{ + /** + * Properties of the object have their normal functionality + * when accessed as list (var_dump, foreach, etc.). + */ + const STD_PROP_LIST = 1; + + /** + * Entries can be accessed as properties (read and write). + */ + const ARRAY_AS_PROPS = 2; + + /** + * @var array + */ + protected $storage; + + /** + * @var int + */ + protected $flag; + + /** + * @var string + */ + protected $iteratorClass; + + /** + * @var array + */ + protected $protectedProperties; + /** - * ArrayObject - * This ArrayObject is a rewrite of the implementation to fix - * issues with php's implementation of ArrayObject where you - * are unable to unset multi-dimensional arrays because you - * need to fetch the properties / lists as references. + * Constructor + * + * @param array $input + * @param int $flags + * @param string $iteratorClass + * @return ArrayObject */ - class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable + public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') { - /** - * Properties of the object have their normal functionality - * when accessed as list (var_dump, foreach, etc.). - */ - const STD_PROP_LIST = 1; - - /** - * Entries can be accessed as properties (read and write). - */ - const ARRAY_AS_PROPS = 2; - - /** - * @var array - */ - protected $storage; - - /** - * @var int - */ - protected $flag; - - /** - * @var string - */ - protected $iteratorClass; - - /** - * @var array - */ - protected $protectedProperties; - - /** - * Constructor - * - * @param array $input - * @param int $flags - * @param string $iteratorClass - * @return ArrayObject - */ - public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') - { - $this->setFlags($flags); - $this->storage = $input; - $this->setIteratorClass($iteratorClass); - $this->protectedProperties = array_keys(get_object_vars($this)); + $this->setFlags($flags); + $this->storage = $input; + $this->setIteratorClass($iteratorClass); + $this->protectedProperties = array_keys(get_object_vars($this)); + } + + /** + * Returns whether the requested key exists + * + * @return boolean + */ + public function __isset($key) + { + if ($this->flag == self::ARRAY_AS_PROPS) { + return $this->offsetExists($key); + } + if (in_array($key, $this->protectedProperties)) { + throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); } - /** - * Returns whether the requested key exists - * - * @return boolean - */ - public function __isset($key) - { - if ($this->flag == self::ARRAY_AS_PROPS) { - return $this->offsetExists($key); - } - if (in_array($key, $this->protectedProperties)) { - throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); - } + return isset($this->$key); + } - return isset($this->$key); + /** + * Sets the value at the specified key to value + * + * @param mixed $key + * @param mixed $value + * @return void + */ + public function __set($key, $value) + { + if ($this->flag == self::ARRAY_AS_PROPS) { + return $this->offsetSet($key, $value); } - - /** - * Sets the value at the specified key to value - * - * @param mixed $key - * @param mixed $value - * @return void - */ - public function __set($key, $value) - { - if ($this->flag == self::ARRAY_AS_PROPS) { - return $this->offsetSet($key, $value); - } - if (in_array($key, $this->protectedProperties)) { - throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); - } - $this->$key = $value; + if (in_array($key, $this->protectedProperties)) { + throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); } + $this->$key = $value; + } - /** - * Unsets the value at the specified key - * - * @param mixed $key - * @return void - */ - public function __unset($key) - { - if ($this->flag == self::ARRAY_AS_PROPS) { - return $this->offsetUnset($key); - } - if (in_array($key, $this->protectedProperties)) { - throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); - } - unset($this->$key); + /** + * Unsets the value at the specified key + * + * @param mixed $key + * @return void + */ + public function __unset($key) + { + if ($this->flag == self::ARRAY_AS_PROPS) { + return $this->offsetUnset($key); + } + if (in_array($key, $this->protectedProperties)) { + throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); } + unset($this->$key); + } - /** - * Returns the value at the specified key by reference - * - * @param mixed $key - * @return mixed - */ - public function &__get($key) - { - $ret = null; - if ($this->flag == self::ARRAY_AS_PROPS) { - $ret =& $this->offsetGet($key); - - return $ret; - } - if (in_array($key, $this->protectedProperties)) { - throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); - } + /** + * Returns the value at the specified key by reference + * + * @param mixed $key + * @return mixed + */ + public function &__get($key) + { + $ret = null; + if ($this->flag == self::ARRAY_AS_PROPS) { + $ret =& $this->offsetGet($key); - return $this->$key; + return $ret; } - - /** - * Appends the value - * - * @param mixed $value - * @return void - */ - public function append($value) - { - $this->storage[] = $value; + if (in_array($key, $this->protectedProperties)) { + throw new Exception\InvalidArgumentException('$key is a protected property, use a different key'); } - /** - * Sort the entries by value - * - * @return void - */ - public function asort() - { - asort($this->storage); - } + return $this->$key; + } - /** - * Get the number of public properties in the ArrayObject - * - * @return int - */ - public function count() - { - return count($this->storage); - } + /** + * Appends the value + * + * @param mixed $value + * @return void + */ + public function append($value) + { + $this->storage[] = $value; + } - /** - * Exchange the array for another one. - * - * @param array $data - * @return array - */ - public function exchangeArray(array $data) - { - $storage = $this->storage; + /** + * Sort the entries by value + * + * @return void + */ + public function asort() + { + asort($this->storage); + } - $this->storage = $data; + /** + * Get the number of public properties in the ArrayObject + * + * @return int + */ + public function count() + { + return count($this->storage); + } - return $storage; - } + /** + * Exchange the array for another one. + * + * @param array $data + * @return array + */ + public function exchangeArray(array $data) + { + $storage = $this->storage; - /** - * Creates a copy of the ArrayObject. - * - * @return array - */ - public function getArrayCopy() - { - return $this->storage; - } + $this->storage = $data; - /** - * Gets the behavior flags. - * - * @return int - */ - public function getFlags() - { - return $this->flag; - } + return $storage; + } - /** - * Create a new iterator from an ArrayObject instance - * - * @return Iterator - */ - public function getIterator() - { - $class = $this->iteratorClass; + /** + * Creates a copy of the ArrayObject. + * + * @return array + */ + public function getArrayCopy() + { + return $this->storage; + } - return new $class($this->storage); - } + /** + * Gets the behavior flags. + * + * @return int + */ + public function getFlags() + { + return $this->flag; + } - /** - * Gets the iterator classname for the ArrayObject. - * - * @return string - */ - public function getIteratorClass() - { - return $this->iteratorClass; - } + /** + * Create a new iterator from an ArrayObject instance + * + * @return Iterator + */ + public function getIterator() + { + $class = $this->iteratorClass; - /** - * Sort the entries by key - * - * @return void - */ - public function ksort() - { - ksort($this->storage); - } + return new $class($this->storage); + } - /** - * Sort an array using a case insensitive "natural order" algorithm - * - * @return void - */ - public function natcasesort() - { - natcasesort($this->storage); - } + /** + * Gets the iterator classname for the ArrayObject. + * + * @return string + */ + public function getIteratorClass() + { + return $this->iteratorClass; + } - /** - * Sort entries using a "natural order" algorithm - * - * @return void - */ - public function natsort() - { - natsort($this->storage); - } + /** + * Sort the entries by key + * + * @return void + */ + public function ksort() + { + ksort($this->storage); + } - /** - * Returns whether the requested key exists - * - * @return boolean - */ - public function offsetExists($key) - { - return isset($this->storage[$key]); - } + /** + * Sort an array using a case insensitive "natural order" algorithm + * + * @return void + */ + public function natcasesort() + { + natcasesort($this->storage); + } - /** - * Returns the value at the specified key - * - * @param mixed $key - * @return mixed - */ - public function &offsetGet($key) - { - $ret = null; - if (!$this->offsetExists($key)) { - return $ret; - } - $ret =& $this->storage[$key]; + /** + * Sort entries using a "natural order" algorithm + * + * @return void + */ + public function natsort() + { + natsort($this->storage); + } + + /** + * Returns whether the requested key exists + * + * @return boolean + */ + public function offsetExists($key) + { + return isset($this->storage[$key]); + } + /** + * Returns the value at the specified key + * + * @param mixed $key + * @return mixed + */ + public function &offsetGet($key) + { + $ret = null; + if (!$this->offsetExists($key)) { return $ret; } + $ret =& $this->storage[$key]; - /** - * Sets the value at the specified key to value - * - * @param mixed $key - * @param mixed $value - * @return void - */ - public function offsetSet($key, $value) - { - $this->storage[$key] = $value; - } + return $ret; + } - /** - * Unsets the value at the specified key - * - * @return void - */ - public function offsetUnset($key) - { - if ($this->offsetExists($key)) { - unset($this->storage[$key]); - } - } + /** + * Sets the value at the specified key to value + * + * @param mixed $key + * @param mixed $value + * @return void + */ + public function offsetSet($key, $value) + { + $this->storage[$key] = $value; + } - /** - * Serialize an ArrayObject - * - * @return string - */ - public function serialize() - { - return serialize(get_object_vars($this)); + /** + * Unsets the value at the specified key + * + * @return void + */ + public function offsetUnset($key) + { + if ($this->offsetExists($key)) { + unset($this->storage[$key]); } + } + + /** + * Serialize an ArrayObject + * + * @return string + */ + public function serialize() + { + return serialize(get_object_vars($this)); + } + + /** + * Sets the behavior flags + * + * @param int $flags + * @return void + */ + public function setFlags($flags) + { + $this->flag = $flags; + } + + /** + * Sets the iterator classname for the ArrayObject + * + * @param string $class + * @return void + */ + public function setIteratorClass($class) + { + if (class_exists($class)) { + $this->iteratorClass = $class; - /** - * Sets the behavior flags - * - * @param int $flags - * @return void - */ - public function setFlags($flags) - { - $this->flag = $flags; + return ; } - /** - * Sets the iterator classname for the ArrayObject - * - * @param string $class - * @return void - */ - public function setIteratorClass($class) - { + if (strpos($class, '\\') === 0) { + $class = '\\' . $class; if (class_exists($class)) { $this->iteratorClass = $class; return ; } - - if (strpos($class, '\\') === 0) { - $class = '\\' . $class; - if (class_exists($class)) { - $this->iteratorClass = $class; - - return ; - } - } - - throw new Exception\InvalidArgumentException('The iterator class does not exist'); } - /** - * Sort the entries with a user-defined comparison function and maintain key association - * - * @param callable $function - * @return void - */ - public function uasort($function) - { - if (is_callable($function)) { - uasort($this->storage, $function); - } - } + throw new Exception\InvalidArgumentException('The iterator class does not exist'); + } - /** - * Sort the entries by keys using a user-defined comparison function - * - * @param callable $function - * @return void - */ - public function uksort($function) - { - if (is_callable($function)) { - uksort($this->storage, $function); - } + /** + * Sort the entries with a user-defined comparison function and maintain key association + * + * @param callable $function + * @return void + */ + public function uasort($function) + { + if (is_callable($function)) { + uasort($this->storage, $function); } + } - /** - * Unserialize an ArrayObject - * - * @param string $data - * @return void - */ - public function unserialize($data) - { - $ar = unserialize($data); - $this->setFlags($ar['flag']); - $this->exchangeArray($ar['storage']); - $this->setIteratorClass($ar['iteratorClass']); - foreach ($ar as $k => $v) { - switch ($k) { - case 'flag': - $this->setFlags($v); - break; - case 'storage': - $this->exchangeArray($v); - break; - case 'iteratorClass': - $this->setIteratorClass($v); - break; - case 'protectedProperties': - continue; - default: - $this->__set($k, $v); - } - } + /** + * Sort the entries by keys using a user-defined comparison function + * + * @param callable $function + * @return void + */ + public function uksort($function) + { + if (is_callable($function)) { + uksort($this->storage, $function); } } -} else { - class ArrayObject extends PhpArrayObject + + /** + * Unserialize an ArrayObject + * + * @param string $data + * @return void + */ + public function unserialize($data) { - /** - * Constructor - * - * @param array $input - * @param int $flags - * @param string $iteratorClass - * @return ArrayObject - */ - public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') - { - parent::__construct($input, $flags, $iteratorClass); + $ar = unserialize($data); + $this->setFlags($ar['flag']); + $this->exchangeArray($ar['storage']); + $this->setIteratorClass($ar['iteratorClass']); + foreach ($ar as $k => $v) { + switch ($k) { + case 'flag': + $this->setFlags($v); + break; + case 'storage': + $this->exchangeArray($v); + break; + case 'iteratorClass': + $this->setIteratorClass($v); + break; + case 'protectedProperties': + continue; + default: + $this->__set($k, $v); + } } } } diff --git a/src/compatibility/ArrayObject.php b/src/compatibility/ArrayObject.php new file mode 100644 index 000000000..ec9316c55 --- /dev/null +++ b/src/compatibility/ArrayObject.php @@ -0,0 +1,36 @@ + 5.3.3, we need to provide a stub for 5.3.3. This stub + * simply extends the PHP ArrayObject implementation, and provides default + * behavior in the constructor. + */ +class ArrayObject extends PhpArrayObject +{ + /** + * Constructor + * + * @param array $input + * @param int $flags + * @param string $iteratorClass + * @return ArrayObject + */ + public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') + { + parent::__construct($input, $flags, $iteratorClass); + } +} diff --git a/src/compatibility/autoload.php b/src/compatibility/autoload.php new file mode 100644 index 000000000..e0b13a781 --- /dev/null +++ b/src/compatibility/autoload.php @@ -0,0 +1,4 @@ +