This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sequenzia/lib/Rails/ArrayHelper/Base.php

50 lines
1.2 KiB
PHP
Raw Normal View History

2013-10-02 18:14:53 +02:00
<?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;
}
}