HummingBirdAnimeClient/src/Aviat/AnimeClient/Config.php

93 lines
1.4 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
use InvalidArgumentException;
/**
* Wrapper for configuration values
*/
class Config {
2015-10-15 09:25:30 -04:00
use \Aviat\Ion\ArrayWrapper;
/**
* Config object
*
* @var Aviat\Ion\Type\ArrayType
*/
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 = [])
{
$this->map = $this->arr($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))
{
return $this->map->get_deep_key($key);
2015-10-15 09:25:30 -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))
{
$this->map->set_deep_key($key, NULL);
2015-10-15 09:25:30 -04:00
}
else
{
$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
*
* @param integer|string|array $key
2015-10-06 11:38:20 -04:00
* @param mixed $value
* @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))
{
$this->map->set_deep_key($key, $value);
}
2015-10-19 13:26:50 -04:00
else if (is_scalar($key) && ! empty($key))
{
$this->map->set($key, $value);
2015-10-15 09:25:30 -04:00
}
2015-10-16 12:53:55 -04: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;
}
}
// End of config.php