Snippets
Created by
Nathan Davis
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | <?php
namespace REACH\Runtime;
/**
* A generic PropertyBag implementation.
*
* @package REACH\Runtime
*/
class PropertyBag implements \ArrayAccess, \Iterator, \Countable {
/**
* @var array $properties the property bag.
*/
private $properties = [];
/**
* @var (array|null) $iterator_keys keys temporarily saved for iterator
*/
private $iterator_keys = null;
/**
* Sets a property value on the PropertyBag object.
* @param string $name the name of the property to set
* @param mixed $value the value to set the property to
*/
public function __set($name, $value) {
$this->properties[$name] = $value;
}
/**
* Acquires the value of a property on the PropertyBag.
*
* @param $name
* @return mixed
* @throws \Exception
*/
public function __get($name) {
if(!array_key_exists($name, $this->properties)) {
throw new \Exception("index `{$name}` does not exist");
}
return $this->properties[$name];
}
/**
* Checks if the PropertyBag has a specific property.
*
* @param string $name name of the property to check if isset.
* @return bool
*/
public function __isset($name) {
return isset($this->properties[$name]);
}
/**
* Deletes a property from the Context.
*
* @param string $name
*/
public function __unset($name) {
unset($this->properties[$name]);
}
/**
* Converts the PropertyBag into a plain associative array.
* @param boolean $eager_load_properties if true, automatically execute callables
* @return array
*/
public function to_array($eager_load_properties = false) {
if($eager_load_properties) {
foreach($this as $key => $value) {
if(is_callable($value)) {
$this->__set($key, call_user_func($value));
}
}
}
return $this->properties;
}
/**
* Serializes the PropertyBag into a string.
* @param boolean $eager_load_properties if true, automatically execute callables
* @param callable|null $serializer serializer function, accepts one parameter, the object to serialize
* @return string
*/
public function serialize($eager_load_properties = false, Callable $serializer = null) {
if(empty($serializer)) {
$serializer = 'json_encode';
}
return call_user_func($serializer, $this->to_array($eager_load_properties));
}
/**
* Returns the properties defined on the Context.
* @return array
*/
public function keys(): array {
return array_keys($this->properties);
}
/**
* Returns the values of the properties defined on the Context;
* @return array
*/
public function values(): array {
return array_values($this->properties);
}
/**
* @inheritDoc
*/
public function current() {
return $this->__get($this->iterator_keys[0]);
}
/**
* @inheritDoc
*/
public function next() {
array_shift($this->iterator_keys);
}
/**
* @inheritDoc
*/
public function key() {
return $this->iterator_keys[0];
}
/**
* @inheritDoc
*/
public function valid() {
return count($this->iterator_keys) > 0;
}
/**
* @inheritDoc
*/
public function rewind() {
$this->iterator_keys = array_keys($this->properties);
}
/**
* @inheritDoc
*/
public function offsetExists($offset) {
return $this->__isset($offset);
}
/**
* @inheritDoc
* @throws \Exception
*/
public function offsetGet($offset) {
return $this->__get($offset);
}
/**
* @inheritDoc
*/
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
/**
* @inheritDoc
*/
public function offsetUnset($offset) {
$this->__unset($offset);
}
/**
* @inheritDoc
*/
public function count() {
return count(array_keys($this->properties));
}
}
|
Empty file.
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.