From fa358c83d090af4be2f3d2a866b27a2cbfa2dfd6 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 21 Aug 2018 01:14:51 +0300 Subject: [PATCH] refactoring of config class, get and set methods added, class migrated to array based conseption --- src/Config.php | 115 ++++++++++++++++++++++++++----------- src/Interfaces/ConfigInterface.php | 59 +++++++++++++++++++ 2 files changed, 141 insertions(+), 33 deletions(-) create mode 100644 src/Interfaces/ConfigInterface.php diff --git a/src/Config.php b/src/Config.php index c94ec32..9d02c33 100644 --- a/src/Config.php +++ b/src/Config.php @@ -2,53 +2,102 @@ namespace RouterOS; -class Config +use RouterOS\Exceptions\Exception; +use RouterOS\Interfaces\ConfigInterface; + +/** + * Class Config + * @package RouterOS + * @since 0.1 + */ +class Config implements ConfigInterface { /** - * Address of Mikrotik Router - * @var string + * Array of parameters (with defaults) + * @var array */ - public $host; + private $_parameters = [ + 'legacy' => Client::LEGACY, + 'ssl' => Client::SSL, + 'timeout' => Client::TIMEOUT, + 'attempts' => Client::ATTEMPTS, + 'delay' => Client::ATTEMPTS_DELAY + ]; /** - * Account's username - * @var string + * Set parameter into array + * + * @param string $name + * @param mixed $value + * @return ConfigInterface */ - public $user; + public function set(string $name, $value): ConfigInterface + { + try { - /** - * Password - * @var string - */ - public $pass; + // Check if parameter in list of allowed parameters + if (!array_key_exists($name, self::ALLOWED)) { + throw new Exception("Requested parameter \"$name\" not found in allowed list [" . implode(',', + array_keys(self::ALLOWED)) . ']'); + } - /** - * Number of port for access - * @var int - */ - public $port = Client::PORT; + // Get type of current variable + $whatType = \gettype($value); + // Get allowed type of parameter + $type = self::ALLOWED[$name]; + $isType = 'is_' . $type; - /** - * Enable ssl support - * @var bool - */ - public $ssl = Client::SSL; + // Check what type has this value + if (!$isType($value)) { + throw new Exception("Parameter \"$name\" has wrong type \"$whatType\" but should be \"$type\""); + } - /** - * Default timeout - * @var int - */ - public $timeout = Client::TIMEOUT; + } catch (Exception $e) { + // __construct + } + + // Save value to array + $this->_parameters[$name] = $value; + + return $this; + } /** - * Count of attempts - * @var int + * Return parameter of current config by name + * + * @param string $parameter + * @return mixed */ - public $attempts = Client::ATTEMPTS; + public function get(string $parameter) + { + try { + // Check if parameter in list of allowed parameters + if (!array_key_exists($parameter, self::ALLOWED)) { + throw new Exception("Requested parameter \"$parameter\" is not found in allowed list [" . implode(',', + array_keys(self::ALLOWED)) . ']'); + } + } catch (Exception $e) { + // __construct + } + + // If client need port number and port is not set + if ($parameter === 'port' && !isset($this->_parameters['port'])) { + // then use default with or without ssl encryption + return (isset($this->_parameters['ssl']) && $this->_parameters['ssl']) + ? Client::PORT_SSL + : Client::PORT; + } + + return $this->_parameters[$parameter]; + } /** - * Delay between attempts - * @var int + * Return array with all parameters of configuration + * + * @return array */ - public $delay = Client::ATTEMPTS_DELAY; + public function getParameters(): array + { + return $this->_parameters; + } } diff --git a/src/Interfaces/ConfigInterface.php b/src/Interfaces/ConfigInterface.php new file mode 100644 index 0000000..fdd6d37 --- /dev/null +++ b/src/Interfaces/ConfigInterface.php @@ -0,0 +1,59 @@ + 'string', + // Username + 'user' => 'string', + // Password + 'pass' => 'string', + // RouterOS API port number for access (if not set use default or default with SSL if SSL enabled) + 'port' => 'int', + // Enable ssl support (if port is not set this parameter must change default port to ssl port) + 'ssl' => 'bool', + // Support of legacy login scheme (true - pre 6.43, false - post 6.43) + 'legacy' => 'bool', + // Max timeout for answer from RouterOS + 'timeout' => 'int', + // Count of attempts to establish TCP session + 'attempts' => 'int', + // Delay between attempts in seconds + 'delay' => 'int', + ]; + + /** + * Set parameter into array + * + * @param string $name + * @param mixed $value + * @return ConfigInterface + */ + public function set(string $name, $value): ConfigInterface; + + /** + * Return parameter of current config by name + * + * @param string $parameter + * @return mixed + */ + public function get(string $parameter); + + /** + * Return array with all parameters of configuration + * + * @return array + */ + public function getParameters(): array; +}