From 0d9a1cc53fb8b23ff1da19ccc3a6ad6ca179c51e Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Wed, 6 Feb 2019 04:14:18 +0300 Subject: [PATCH] via client constructor is not possible to create object with settings from array --- src/Config.php | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Config.php b/src/Config.php index 7bbdfd2..0912a1a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -7,44 +7,59 @@ use RouterOS\Interfaces\ConfigInterface; /** * Class Config with array of parameters + * * @package RouterOS - * @since 0.1 + * @since 0.1 */ class Config implements ConfigInterface { /** * Array of parameters (with some default values) + * * @var array */ private $_parameters = [ - 'legacy' => Client::LEGACY, - 'ssl' => Client::SSL, - 'timeout' => Client::TIMEOUT, + 'legacy' => Client::LEGACY, + 'ssl' => Client::SSL, + 'timeout' => Client::TIMEOUT, 'attempts' => Client::ATTEMPTS, - 'delay' => Client::ATTEMPTS_DELAY + 'delay' => Client::ATTEMPTS_DELAY ]; /** + * 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 * * @param string $key - * @param array $array + * @param array $array * @throws ConfigException */ private function exceptionIfKeyNotExist(string $key, array $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)) . ']'); } } /** * Compare data types of some value * - * @param string $name Name of value - * @param mixed $whatType What type has value - * @param mixed $isType What type should be + * @param string $name Name of value + * @param mixed $whatType What type has value + * @param mixed $isType What type should be * @throws ConfigException */ private function exceptionIfTypeMismatch(string $name, $whatType, $isType) @@ -58,11 +73,11 @@ class Config implements ConfigInterface * Set parameter into array * * @param string $name - * @param mixed $value - * @return ConfigInterface + * @param mixed $value + * @return \RouterOS\Config * @throws ConfigException */ - public function set(string $name, $value): ConfigInterface + public function set(string $name, $value): Config { // Check of key in array $this->exceptionIfKeyNotExist($name, self::ALLOWED); @@ -98,10 +113,10 @@ class Config implements ConfigInterface * Remove parameter from array by name * * @param string $parameter - * @return ConfigInterface + * @return \RouterOS\Config * @throws ConfigException */ - public function delete(string $parameter): ConfigInterface + public function delete(string $parameter): Config { // Check of key in array $this->exceptionIfKeyNotExist($parameter, self::ALLOWED);