Skip to content

Instantly share code, notes, and snippets.

@kinncj
Last active September 21, 2015 15:51
Show Gist options
  • Save kinncj/13127f31f7a3c190f792 to your computer and use it in GitHub Desktop.
Save kinncj/13127f31f7a3c190f792 to your computer and use it in GitHub Desktop.
Map
<?php
class Map extends ArrayObject
{
public function __construct($input = [], $flags = 0, $iterator_class = "ArrayIterator")
{
parent::__construct([], $flags, $iterator_class);
foreach ($input as $k=>$v) {
$this[$k] = $v;
}
}
public function offSetSet($index, $value)
{
if (is_string($index)) {
$index = trim($index);
}
if (!is_numeric($index) && empty($index)) {
throw new \InvalidArgumentException(sprintf('A map key can\'t be empty [%s]', $index));
}
parent::offSetSet($index, $value);
}
public function getArrayCopy()
{
if (empty((array) $this)) {
return new \stdClass();
}
return parent::getArrayCopy();
}
public function append($value)
{
throw new \BadMethodCallException("You can't append to a map");
}
public function exchangeArray(array $input)
{
parent::exchangeArray([]);
foreach ($input as $k=>$v) {
$this[$k] = $v;
}
return (array) $this;
}
public function __get($item)
{
return $this->offSetGet($item);
}
public function __set($item, $value)
{
$this->offSetSet($item, $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment