HummingBirdAnimeClient/src/Aviat/AnimeClient/Config.php

134 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* Base Configuration class
*/
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
2015-06-26 16:39:10 -04:00
/**
* Wrapper for configuration values
*/
class Config {
2015-10-15 09:25:30 -04:00
use \Aviat\Ion\ArrayWrapper;
/**
* Config object
*
* @var array
*/
2015-10-09 14:34:55 -04:00
protected $map = [];
/**
* Constructor
*
* @param array $config_array
*/
2015-10-09 14:34:55 -04:00
public function __construct(array $config_array = [])
{
2015-10-09 14:34:55 -04:00
$this->map = $config_array;
}
/**
2015-10-06 11:38:20 -04:00
* Get a config value
*
2015-10-15 09:25:30 -04:00
* @param array|string $key
* @return mixed
*/
2015-10-06 11:38:20 -04:00
public function get($key)
{
2015-10-15 09:25:30 -04:00
if (is_array($key))
{
2015-10-15 10:23:00 -04:00
return $this->arr($this->map)->get_deep_key($key);
2015-10-15 09:25:30 -04:00
}
2015-10-09 14:34:55 -04:00
if (array_key_exists($key, $this->map))
{
2015-10-09 14:34:55 -04:00
return $this->map[$key];
}
return NULL;
}
2015-10-06 11:38:20 -04:00
2015-10-15 09:25:30 -04:00
/**
* Return a reference to an arbitrary key on the config map
* @param array $key
* @param bool $create Whether to create the missing array keys
* @return mixed
*/
protected function &get_deep_key(array $key, $create = TRUE)
{
$pos =& $this->map;
// Create the start of the array if it doesn't exist
if ($create && ! is_array($pos))
{
$pos = [];
}
elseif ( ! is_array($pos))
{
return NULL;
}
// Iterate through the levels of the array,
// create the levels if they don't exist
foreach($key as $level)
{
if ($create && empty($pos) && ! is_array($pos))
{
$pos = [];
$pos[$level] = [];
}
$pos =& $pos[$level];
}
return $pos;
}
/**
* Remove a config value
*
* @param string|array $key
* @return void
*/
public function delete($key)
{
$pos =& $this->map;
if (is_array($key))
{
$pos =& $this->arr($this->map)->get_deep_key($key);
}
else
{
$pos =& $this->map[$key];
}
2015-10-15 10:23:00 -04:00
$pos = NULL;
2015-10-15 09:25:30 -04:00
}
2015-10-06 11:38:20 -04:00
/**
* Set a config value
*
2015-10-15 09:25:30 -04:00
* @param string|array $key
2015-10-06 11:38:20 -04:00
* @param mixed $value
* @return Config
*/
public function set($key, $value)
{
2015-10-15 09:25:30 -04:00
$pos =& $this->map;
if (is_array($key))
{
$pos =& $this->get_deep_key($key);
$pos = $value;
}
else
{
$pos[$key] = $value;
}
2015-10-06 11:38:20 -04:00
return $this;
}
}
// End of config.php