Browse Source

via client constructor is not possible to create object with settings from array

tags/0.6
Paul Rock 7 years ago
parent
commit
0d9a1cc53f
  1. 27
      src/Config.php

27
src/Config.php

@ -7,6 +7,7 @@ use RouterOS\Interfaces\ConfigInterface;
/** /**
* Class Config with array of parameters * Class Config with array of parameters
*
* @package RouterOS * @package RouterOS
* @since 0.1 * @since 0.1
*/ */
@ -14,6 +15,7 @@ class Config implements ConfigInterface
{ {
/** /**
* Array of parameters (with some default values) * Array of parameters (with some default values)
*
* @var array * @var array
*/ */
private $_parameters = [ private $_parameters = [
@ -25,6 +27,20 @@ class Config implements ConfigInterface
]; ];
/** /**
* Config constructor.
*
* @param array $parameters List of parameters which can be set on object creation stage
* @throws ConfigException
* @since 0.6
*/
public function __construct(array $parameters = [])
{
foreach ($parameters as $key => $value) {
$this->set($key, $value);
}
}
/**
* Check if key in list of parameters * Check if key in list of parameters
* *
* @param string $key * @param string $key
@ -34,8 +50,7 @@ class Config implements ConfigInterface
private function exceptionIfKeyNotExist(string $key, array $array) private function exceptionIfKeyNotExist(string $key, array $array)
{ {
if (!array_key_exists($key, $array)) { if (!array_key_exists($key, $array)) {
throw new ConfigException("Requested parameter '$key' not found in list [" . implode(',',
array_keys($array)) . ']');
throw new ConfigException("Requested parameter '$key' not found in list [" . implode(',', array_keys($array)) . ']');
} }
} }
@ -59,10 +74,10 @@ class Config implements ConfigInterface
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return ConfigInterface
* @return \RouterOS\Config
* @throws ConfigException * @throws ConfigException
*/ */
public function set(string $name, $value): ConfigInterface
public function set(string $name, $value): Config
{ {
// Check of key in array // Check of key in array
$this->exceptionIfKeyNotExist($name, self::ALLOWED); $this->exceptionIfKeyNotExist($name, self::ALLOWED);
@ -98,10 +113,10 @@ class Config implements ConfigInterface
* Remove parameter from array by name * Remove parameter from array by name
* *
* @param string $parameter * @param string $parameter
* @return ConfigInterface
* @return \RouterOS\Config
* @throws ConfigException * @throws ConfigException
*/ */
public function delete(string $parameter): ConfigInterface
public function delete(string $parameter): Config
{ {
// Check of key in array // Check of key in array
$this->exceptionIfKeyNotExist($parameter, self::ALLOWED); $this->exceptionIfKeyNotExist($parameter, self::ALLOWED);

Loading…
Cancel
Save