Browse Source

refactoring of config class, get and set methods added, class migrated to array based conseption

tags/0.2
Paul Rock 7 years ago
parent
commit
fa358c83d0
  1. 115
      src/Config.php
  2. 59
      src/Interfaces/ConfigInterface.php

115
src/Config.php

@ -2,53 +2,102 @@
namespace RouterOS;
class Config
{
/**
* Address of Mikrotik Router
* @var string
*/
public $host;
use RouterOS\Exceptions\Exception;
use RouterOS\Interfaces\ConfigInterface;
/**
* Account's username
* @var string
* Class Config
* @package RouterOS
* @since 0.1
*/
public $user;
class Config implements ConfigInterface
{
/**
* Password
* @var string
* Array of parameters (with defaults)
* @var array
*/
public $pass;
private $_parameters = [
'legacy' => Client::LEGACY,
'ssl' => Client::SSL,
'timeout' => Client::TIMEOUT,
'attempts' => Client::ATTEMPTS,
'delay' => Client::ATTEMPTS_DELAY
];
/**
* Number of port for access
* @var int
* Set parameter into array
*
* @param string $name
* @param mixed $value
* @return ConfigInterface
*/
public $port = Client::PORT;
public function set(string $name, $value): ConfigInterface
{
try {
/**
* Enable ssl support
* @var bool
*/
public $ssl = Client::SSL;
// 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)) . ']');
}
/**
* Default timeout
* @var int
*/
public $timeout = Client::TIMEOUT;
// Get type of current variable
$whatType = \gettype($value);
// Get allowed type of parameter
$type = self::ALLOWED[$name];
$isType = 'is_' . $type;
// Check what type has this value
if (!$isType($value)) {
throw new Exception("Parameter \"$name\" has wrong type \"$whatType\" but should be \"$type\"");
}
} 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;
}
}

59
src/Interfaces/ConfigInterface.php

@ -0,0 +1,59 @@
<?php
namespace RouterOS\Interfaces;
/**
* Interface ConfigInterface
* @package RouterOS\Interfaces
* @since 0.2
*/
interface ConfigInterface
{
/**
* List of allowed parameters of config
*/
const ALLOWED = [
// Address of Mikrotik RouterOS
'host' => '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;
}
Loading…
Cancel
Save