| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Container; |
| 4 |
|
| 5 |
class Container implements \ArrayAccess { |
| 6 |
|
| 7 |
private $values = []; |
| 8 |
private $raw = []; |
| 9 |
private $keys = []; |
| 10 |
|
| 11 |
public function offsetSet($id, $value) { |
| 12 |
$this->values[$id] = $value; |
| 13 |
$this->keys[$id] = true; |
| 14 |
} |
| 15 |
|
| 16 |
public function offsetGet($id) { |
| 17 |
|
| 18 |
if(!isset($this->keys[$id])) { |
| 19 |
throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); |
| 20 |
} |
| 21 |
|
| 22 |
if(isset($this->raw[$id]) || !is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) { |
| 23 |
return $this->values[$id]; |
| 24 |
} |
| 25 |
|
| 26 |
$raw = $this->values[$id]; |
| 27 |
$val = $this->values[$id] = $raw($this); |
| 28 |
$this->raw[$id] = $this->values[$id]; |
| 29 |
|
| 30 |
return $this->values[$id]; |
| 31 |
} |
| 32 |
|
| 33 |
public function offsetExists($id) { |
| 34 |
return isset($this->keys[$id]); |
| 35 |
} |
| 36 |
|
| 37 |
public function offsetUnset($id) { |
| 38 |
if(isset($this->keys[$id])) { |
| 39 |
unset($this->values[$id], $this->raw[$id], $this->keys[$id]); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function keys() { |
| 44 |
return array_keys($this->values); |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|