X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmerry%2FConfig.php;h=e9f4aa572b23680cf94b168d847cc7bcd7644c79;hb=HEAD;hp=a00c976f11c506042c804f39a67c1388c7a224c8;hpb=ab50f5e693dac5c9553e8bfc53ef5e1b2aecc26f;p=m6w6%2Fmerry diff --git a/lib/merry/Config.php b/lib/merry/Config.php index a00c976..e9f4aa5 100644 --- a/lib/merry/Config.php +++ b/lib/merry/Config.php @@ -13,29 +13,8 @@ namespace merry; * @see https://github.com/m6w6/merry * @package merry\Config */ -class Config implements \ArrayAccess, \RecursiveIterator +class Config extends Container { - /** - * Index for a numerically indexed array - * @internal - * @var int - */ - private $index = 0; - - /** - * Container - * @internal - * @var stdClass - */ - private $props; - - /** - * State for the RecursiveIterator - * @internal - * @var array - */ - private $riter; - /** * Create a new configuration container * @param array $array the configuration array @@ -44,8 +23,6 @@ class Config implements \ArrayAccess, \RecursiveIterator * @param string $key_sep a separator for key traversal */ public function __construct(array $array = null, $section = null, $section_sep = ":", $key_sep = ".") { - $this->props = new \stdClass; - if (isset($section) && strlen($section_sep)) { $array = $this->combine($array, $section_sep)[$section]; } @@ -57,10 +34,7 @@ class Config implements \ArrayAccess, \RecursiveIterator $this->walk($config, $key, $val, $key_sep); } } - - foreach ($config as $property => $value) { - $this->__set($property, $value); - } + parent::__construct($config, false); } } @@ -99,173 +73,6 @@ class Config implements \ArrayAccess, \RecursiveIterator } $ptr = $val; } - - /** - * Recursively turn a Config instance and its childs into an array - * @param \merry\Config $o the Config instance to convert to an array - * @return array - */ - protected function arrayify(Config $o) { - $a = []; - - foreach ($o->props as $k => $v) { - if ($v instanceof Config) { - $a[$k] = $this->arrayify($v); - } else { - $a[$k] = $v; - } - } - - return $a; - } - - /** - * Apply one or mor modifier callbacks - * @param mixed $modifier - * @return \merry\Config - */ - public function apply($modifier) { - if (is_callable($modifier)) { - foreach ($this->props as $prop => $value) { - $this->__set($prop, $modifier($value, $prop)); - } - } else { - foreach ($modifier as $key => $mod) { - if (is_callable($mod)) { - $this->props->$key = $mod(isset($this->props->$key) ? $this->props->$key : null, $key); - } elseif (is_array($mod)) { - $this->props->$key->apply($mod); - } else { - /* */ - } - } - } - return $this; - } - - /** - * Return the complete config as array - * @return array - */ - function toArray() { - return $this->arrayify($this); - } - - /** - * @ignore - */ - function __get($prop) { - return $this->props->$prop; - } - - /** - * @ignore - */ - function __set($prop, $value) { - if (isset($value) && !is_scalar($value) && !($value instanceof Config)) { - $value = new static((array) $value); - } - if (!strlen($prop)) { - $prop = $this->index++; - } elseif (is_numeric($prop) && !strcmp($prop, (int) $prop)) { - /* update internal index */ - if ($prop >= $this->index) { - $this->index = $prop + 1; - } - } - - $this->props->$prop = $value; - } - - /** - * @ignore - */ - function __isset($prop) { - return isset($this->props->$prop); - } - - /** - * @ignore - */ - function __unset($prop) { - unset($this->props->$prop); - } - - /** - * @ignore - */ - function offsetGet($o) { - return $this->props->$o; - } - - /** - * @ignore - */ - function offsetSet($o, $v) { - $this->__set($o, $v); - } - - /** - * @ignore - */ - function offsetExists($o) { - return isset($this->props->$o); - } - - /** - * @ignore - */ - function offsetUnset($o) { - unset($this->props->$o); - } - - /** - * @ignore - */ - function rewind() { - $this->riter = (array) $this->props; - reset($this->riter); - } - - /** - * @ignore - */ - function valid() { - return NULL !== key($this->riter); - } - - /** - * @ignore - */ - function next() { - next($this->riter); - } - - /** - * @ignore - */ - function key() { - return key($this->riter); - } - - /** - * @ignore - */ - function current() { - return current($this->riter); - } - - /** - * @ignore - */ - function hasChildren() { - return current($this->riter) instanceof Config; - } - - /** - * @ignore - */ - function getChildren() { - return current($this->riter); - } } + +/* vim: set noet ts=4 sw=4: */