Maslosoft Gazebo API
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/gazebo
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\Gazebo;
use ArrayAccess;
use Countable;
use Iterator;
use Maslosoft\Gazebo\Exceptions\GazeboException;
use ReflectionObject;
use ReflectionProperty;
/**
* Plugin container for easier managing complex array
* and to allow some php docs on otherways hardly documented arrays.
* This restricts array like access to only public defined properties.
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class ConfigContainer implements ArrayAccess, Countable, Iterator
{
/**
* Values holder
* @var mixed[]
*/
private $_values = [];
/**
* Property accessed fields
* @var bool[]
*/
private $_properties = [];
/**
* Create instance with optional configuration.
* This also prevents setting bogus configuration keys.
* This will unset all public properties and pass them thru get/set and allow array access.
* @param mixed[] $config
*/
public function __construct($config = [])
{
$info = new ReflectionObject($this);
foreach ($info->getProperties(ReflectionProperty::IS_PUBLIC) as $property)
{
/* @var $property ReflectionProperty */
if ($property->isStatic())
{
continue;
}
$this->_properties[$property->name] = true;
unset($this->{$property->name});
}
$this->apply($config);
}
/**
* Apply configuration
* @param mixed[] $config
*/
public function apply($config)
{
foreach ($config as $name => $value)
{
$this->offsetSet($name, $value);
}
}
public function has($name)
{
return array_key_exists($name, $this->_properties);
}
// <editor-fold defaultstate="collapsed" desc="__* magic implementation">
/**
* This will be called instead of public getting properties.
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->offsetGet($name);
}
/**
* This will be called instead of public setting properties.
* @param string $name Configuration key value
* @param mixed $value Configuration value
* @return mixed
*/
public function __set($name, $value)
{
$this->offsetSet($name, $value);
}
/**
* Unset
* @param string $name
*/
public function __unset($name)
{
$this->offsetUnset($name);
}
/**
* Isset
* @param string $name
* @return bool
*/
public function __isset($name)
{
return $this->offsetExists($name);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="ArrayAccess implementation">
public function offsetExists($name)
{
return array_key_exists($name, $this->_values);
}
public function offsetGet($name)
{
if (!$this->has($name))
{
throw new GazeboException("Configuration property `$name` does not exists. Tried to get value.");
}
return $this->_values[$name]->toArray();
}
public function offsetSet($name, $value)
{
if (!$this->has($name))
{
throw new GazeboException("Configuration property `$name` does not exists. Tried to set value.");
}
return $this->_values[$name] = new PluginsContainer($value);
}
public function offsetUnset($name)
{
if (!$this->has($name))
{
throw new GazeboException("Configuration property `$name` does not exists. Tried to unset value.");
}
unset($this->_values[$name]);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Countable implementation">
public function count($mode = COUNT_NORMAL)
{
return count($this->_values, $mode);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Iterator implementation">
public function current()
{
return $this->offsetGet(key($this->_values));
}
public function key()
{
return key($this->_values);
}
public function next()
{
return next($this->_values);
}
public function rewind()
{
return reset($this->_values);
}
public function valid()
{
return $this->has($this->key()) && $this->offsetExists($this->key());
}
// </editor-fold>
}
API documentation generated by ApiGen