Last active
September 21, 2015 15:51
-
-
Save kinncj/13127f31f7a3c190f792 to your computer and use it in GitHub Desktop.
Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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