From a1ccb969f45c94dddf9ab9efe8e44441ff4575e2 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Thu, 14 Feb 2019 04:27:56 +0300 Subject: [PATCH] helpers added, exceptions rewriten to helpers --- src/Client.php | 22 +++------------- src/Config.php | 61 ++++++++++++++++----------------------------- src/Helpers/ArrayHelper.php | 42 +++++++++++++++++++++++++++++++ src/Helpers/TypeHelper.php | 25 +++++++++++++++++++ 4 files changed, 92 insertions(+), 58 deletions(-) create mode 100644 src/Helpers/ArrayHelper.php create mode 100644 src/Helpers/TypeHelper.php diff --git a/src/Client.php b/src/Client.php index d9fa056..499ab59 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,7 @@ namespace RouterOS; use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ConfigException; +use RouterOS\Helpers\ArrayHelper; /** * Class Client for RouterOS management @@ -57,7 +58,9 @@ class Client implements Interfaces\ClientInterface } // Check for important keys - $this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); + if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) { + throw new ConfigException("Parameter '$key' of Config is not set or empty"); + } // Save config if everything is okay $this->setConfig($config); @@ -69,23 +72,6 @@ class Client implements Interfaces\ClientInterface } /** - * Check for important keys - * - * @param array $keys - * @param \RouterOS\Config $config - * @throws \RouterOS\Exceptions\ConfigException - */ - private function exceptionIfKeyNotExist(array $keys, Config $config) - { - $parameters = $config->getParameters(); - foreach ($keys as $key) { - if (!array_key_exists($key, $parameters) && isset($parameters[$key])) { - throw new ConfigException("Parameter '$key' of Config is not set or empty"); - } - } - } - - /** * Get some parameter from config * * @param string $parameter Name of required parameter diff --git a/src/Config.php b/src/Config.php index 0912a1a..a10fe0a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,6 +3,8 @@ namespace RouterOS; use RouterOS\Exceptions\ConfigException; +use RouterOS\Helpers\ArrayHelper; +use RouterOS\Helpers\TypeHelper; use RouterOS\Interfaces\ConfigInterface; /** @@ -41,35 +43,6 @@ class Config implements ConfigInterface } /** - * Check if key in list of parameters - * - * @param string $key - * @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)) . ']'); - } - } - - /** - * 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 - * @throws ConfigException - */ - private function exceptionIfTypeMismatch(string $name, $whatType, $isType) - { - if ($whatType !== $isType) { - throw new ConfigException("Parameter '$name' has wrong type '$whatType' but should be '$isType'"); - } - } - - /** * Set parameter into array * * @param string $name @@ -80,10 +53,14 @@ class Config implements ConfigInterface public function set(string $name, $value): Config { // Check of key in array - $this->exceptionIfKeyNotExist($name, self::ALLOWED); + if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { + throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); + } // Check what type has this value - $this->exceptionIfTypeMismatch($name, \gettype($value), self::ALLOWED[$name]); + if (TypeHelper::checkIfTypeMismatch($name, \gettype($value), self::ALLOWED[$name])) { + throw new ConfigException("Parameter '$name' has wrong type '" . \gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'"); + } // Save value to array $this->_parameters[$name] = $value; @@ -112,17 +89,19 @@ class Config implements ConfigInterface /** * Remove parameter from array by name * - * @param string $parameter + * @param string $name * @return \RouterOS\Config - * @throws ConfigException + * @throws \RouterOS\Exceptions\ConfigException */ - public function delete(string $parameter): Config + public function delete(string $name): Config { // Check of key in array - $this->exceptionIfKeyNotExist($parameter, self::ALLOWED); + if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { + throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); + } // Save value to array - unset($this->_parameters[$parameter]); + unset($this->_parameters[$name]); return $this; } @@ -130,16 +109,18 @@ class Config implements ConfigInterface /** * Return parameter of current config by name * - * @param string $parameter + * @param string $name * @return mixed * @throws ConfigException */ - public function get(string $parameter) + public function get(string $name) { // Check of key in array - $this->exceptionIfKeyNotExist($parameter, self::ALLOWED); + if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { + throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); + } - return $this->getPort($parameter) ?? $this->_parameters[$parameter]; + return $this->getPort($name) ?? $this->_parameters[$name]; } /** diff --git a/src/Helpers/ArrayHelper.php b/src/Helpers/ArrayHelper.php new file mode 100644 index 0000000..796b8e5 --- /dev/null +++ b/src/Helpers/ArrayHelper.php @@ -0,0 +1,42 @@ +