Browse Source

delete method added to config class

tags/0.4
Paul Rock 7 years ago
parent
commit
d875e318c1
  1. 49
      src/Config.php
  2. 20
      src/Interfaces/ConfigInterface.php

49
src/Config.php

@ -46,24 +46,19 @@ class Config implements ConfigInterface
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return ConfigInterface * @return ConfigInterface
* @throws Exception
*/ */
public function set(string $name, $value): ConfigInterface public function set(string $name, $value): ConfigInterface
{ {
try {
// Check of key in array
$this->keyAllowed($name, self::ALLOWED);
$whatType = \gettype($value);
$isType = self::ALLOWED[$name];
// Check of key in array
$this->keyAllowed($name, self::ALLOWED);
// Check what type has this value
if ($whatType !== $isType) {
throw new Exception("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
}
$whatType = \gettype($value);
$isType = self::ALLOWED[$name];
} catch (Exception $e) {
// __construct
// Check what type has this value
if ($whatType !== $isType) {
throw new Exception("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
} }
// Save value to array // Save value to array
@ -91,20 +86,34 @@ class Config implements ConfigInterface
} }
/** /**
* Remove parameter from array by name
*
* @param string $parameter
* @return ConfigInterface
* @throws Exception
*/
public function delete(string $parameter): ConfigInterface
{
// Check of key in array
$this->keyAllowed($parameter, self::ALLOWED);
// Save value to array
unset($this->_parameters[$parameter]);
return $this;
}
/**
* Return parameter of current config by name * Return parameter of current config by name
* *
* @param string $parameter * @param string $parameter
* @return mixed * @return mixed
* @throws Exception
*/ */
public function get(string $parameter) public function get(string $parameter)
{ {
try {
// Check of key in array
$this->keyAllowed($parameter, self::ALLOWED);
} catch (Exception $e) {
// __construct
}
// Check of key in array
$this->keyAllowed($parameter, self::ALLOWED);
return $this->getPort($parameter) ?? $this->_parameters[$parameter]; return $this->getPort($parameter) ?? $this->_parameters[$parameter];
} }

20
src/Interfaces/ConfigInterface.php

@ -20,17 +20,17 @@ interface ConfigInterface
// Password // Password
'pass' => 'string', 'pass' => 'string',
// RouterOS API port number for access (if not set use default or default with SSL if SSL enabled) // RouterOS API port number for access (if not set use default or default with SSL if SSL enabled)
'port' => 'int',
'port' => 'integer',
// Enable ssl support (if port is not set this parameter must change default port to ssl port) // Enable ssl support (if port is not set this parameter must change default port to ssl port)
'ssl' => 'bool',
'ssl' => 'boolean',
// Support of legacy login scheme (true - pre 6.43, false - post 6.43) // Support of legacy login scheme (true - pre 6.43, false - post 6.43)
'legacy' => 'bool',
'legacy' => 'boolean',
// Max timeout for answer from RouterOS // Max timeout for answer from RouterOS
'timeout' => 'int',
'timeout' => 'integer',
// Count of attempts to establish TCP session // Count of attempts to establish TCP session
'attempts' => 'int',
'attempts' => 'integer',
// Delay between attempts in seconds // Delay between attempts in seconds
'delay' => 'int',
'delay' => 'integer',
]; ];
/** /**
@ -43,6 +43,14 @@ interface ConfigInterface
public function set(string $name, $value): ConfigInterface; public function set(string $name, $value): ConfigInterface;
/** /**
* Remove parameter from array by name
*
* @param string $parameter
* @return ConfigInterface
*/
public function delete(string $parameter): ConfigInterface;
/**
* Return parameter of current config by name * Return parameter of current config by name
* *
* @param string $parameter * @param string $parameter

Loading…
Cancel
Save