Sequenzia/lib/Rails/ArrayHelper/Base.php
2013-10-02 11:14:53 -05:00

50 lines
1.2 KiB
PHP
Executable File

<?php
namespace Rails\ArrayHelper;
abstract class Base implements \ArrayAccess, \IteratorAggregate
{
abstract protected function _get_array();
public function offsetSet($offset, $value)
{
if ($offset === null)
$this->_get_array()[] = $value;
else
$this->_get_array()[$offset] = $value;
}
public function offsetExists($offset)
{
return isset($this->_get_array()[$offset]);
}
public function offsetUnset($offset)
{
unset($this->_get_array()[$offset]);
}
public function offsetGet($offset)
{
if ($this->offsetExists($offset))
return $this->_get_array()[$offset];
return null;
}
public function getIterator()
{
return new ArrayIterator($this->_get_array());
}
public function merge()
{
foreach (func_get_args() as $arr) {
if (!is_array($arr))
throw new Exception\InvalidArgumentException(
sprintf("All arguments passed to merge() must be array, %s passed", gettype($arr))
);
foreach ($arr as $k => $v)
$this->offsetSet($k, $v);
}
return $this;
}
}