Created
February 5, 2018 11:55
-
-
Save jmsfwk/4d9f0f6de95f9d34f7d714f52c05b636 to your computer and use it in GitHub Desktop.
Class to send data to template that tries to avoid exceptions when data does not exists
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 | |
use ArrayAccess; | |
use ArrayIterator; | |
use IteratorAggregate; | |
class Context implements ArrayAccess, IteratorAggregate | |
{ | |
/** @var array */ | |
private $data; | |
public function __construct(array $data = []) | |
{ | |
$this->data = $data; | |
} | |
public function getIterator() | |
{ | |
return new ArrayIterator($this->data); | |
} | |
public function offsetExists($offset) | |
{ | |
return array_key_exists($offset, $this->data); | |
} | |
public function offsetGet($offset) | |
{ | |
if (array_key_exists($offset, $this->data)) { | |
return $this->data[$offset]; | |
} | |
return new static(); | |
} | |
public function offsetSet($offset, $value) | |
{ | |
$this->data[$offset] = $value; | |
} | |
public function offsetUnset($offset) | |
{ | |
unset($this->data[$offset]); | |
} | |
public function __get($name) | |
{ | |
return $this[$name]; | |
} | |
public function __set($name, $value) | |
{ | |
return $this[$name] = $value; | |
} | |
public function __isset($name) | |
{ | |
return $this->offsetExists($name); | |
} | |
public function __toString(): string | |
{ | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment