2015-06-26 12:03:42 -04:00
|
|
|
<?php
|
2015-09-14 10:54:50 -04:00
|
|
|
/**
|
2016-07-28 10:44:13 -04:00
|
|
|
* Ion
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-07-28 10:44:13 -04:00
|
|
|
* Building blocks for web development
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-07-28 10:44:13 -04:00
|
|
|
* @package Ion
|
2015-11-16 11:40:01 -05:00
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2015-11-16 11:40:01 -05:00
|
|
|
* @license MIT
|
2015-09-14 10:54:50 -04:00
|
|
|
*/
|
2015-06-26 12:03:42 -04:00
|
|
|
|
2016-07-28 10:44:13 -04:00
|
|
|
namespace Aviat\Ion;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2016-07-28 10:44:13 -04:00
|
|
|
|
|
|
|
use Aviat\Ion\Exception\ConfigException;
|
2016-07-27 13:18:52 -04:00
|
|
|
|
2015-10-15 22:00:09 -04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
/**
|
|
|
|
* Wrapper for configuration values
|
|
|
|
*/
|
2016-07-27 13:18:52 -04:00
|
|
|
class Config implements ConfigInterface {
|
2015-06-26 12:03:42 -04:00
|
|
|
|
2016-07-28 10:44:13 -04:00
|
|
|
use ArrayWrapper;
|
2015-10-15 09:25:30 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
/**
|
|
|
|
* Config object
|
|
|
|
*
|
2015-11-04 16:53:22 -05:00
|
|
|
* @var \Aviat\Ion\Type\ArrayType
|
2015-06-26 12:03:42 -04:00
|
|
|
*/
|
2015-10-09 14:34:55 -04:00
|
|
|
protected $map = [];
|
2015-06-26 12:03:42 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2015-10-09 22:29:59 -04:00
|
|
|
* @param array $config_array
|
2015-06-26 12:03:42 -04:00
|
|
|
*/
|
2015-10-09 14:34:55 -04:00
|
|
|
public function __construct(array $config_array = [])
|
2015-06-26 12:03:42 -04:00
|
|
|
{
|
2015-10-15 22:00:09 -04:00
|
|
|
$this->map = $this->arr($config_array);
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-06 11:38:20 -04:00
|
|
|
* Get a config value
|
2015-06-26 12:03:42 -04:00
|
|
|
*
|
2015-10-15 09:25:30 -04:00
|
|
|
* @param array|string $key
|
2015-06-26 12:03:42 -04:00
|
|
|
* @return mixed
|
2016-07-28 10:44:13 -04:00
|
|
|
* @throws ConfigException
|
2015-06-26 12:03:42 -04:00
|
|
|
*/
|
2015-10-06 11:38:20 -04:00
|
|
|
public function get($key)
|
2015-06-26 12:03:42 -04:00
|
|
|
{
|
2015-10-15 09:25:30 -04:00
|
|
|
if (is_array($key))
|
|
|
|
{
|
2015-10-15 22:00:09 -04:00
|
|
|
return $this->map->get_deep_key($key);
|
2015-10-15 09:25:30 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 22:00:09 -04:00
|
|
|
return $this->map->get($key);
|
2015-10-15 09:25:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a config value
|
|
|
|
*
|
|
|
|
* @param string|array $key
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
if (is_array($key))
|
|
|
|
{
|
2015-10-15 22:00:09 -04:00
|
|
|
$this->map->set_deep_key($key, NULL);
|
2015-10-15 09:25:30 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-15 22:00:09 -04:00
|
|
|
$pos =& $this->map->get($key);
|
|
|
|
$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 22:00:09 -04:00
|
|
|
* @param integer|string|array $key
|
2015-10-06 11:38:20 -04:00
|
|
|
* @param mixed $value
|
2015-10-15 22:00:09 -04:00
|
|
|
* @throws InvalidArgumentException
|
2015-10-06 11:38:20 -04:00
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
2015-10-15 09:25:30 -04:00
|
|
|
if (is_array($key))
|
|
|
|
{
|
2015-10-15 22:00:09 -04:00
|
|
|
$this->map->set_deep_key($key, $value);
|
|
|
|
}
|
2015-10-19 13:26:50 -04:00
|
|
|
else if (is_scalar($key) && ! empty($key))
|
2015-10-15 22:00:09 -04:00
|
|
|
{
|
|
|
|
$this->map->set($key, $value);
|
2015-10-15 09:25:30 -04:00
|
|
|
}
|
2015-11-05 10:41:46 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException("Key must be integer, string, or array, and cannot be empty");
|
|
|
|
}
|
2015-10-15 09:25:30 -04:00
|
|
|
|
2015-10-06 11:38:20 -04:00
|
|
|
return $this;
|
|
|
|
}
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
|
|
|
// End of config.php
|