Browse Source

helpers added, exceptions rewriten to helpers

tags/0.7
Paul Rock 7 years ago
parent
commit
a1ccb969f4
  1. 22
      src/Client.php
  2. 61
      src/Config.php
  3. 42
      src/Helpers/ArrayHelper.php
  4. 25
      src/Helpers/TypeHelper.php

22
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

61
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];
}
/**

42
src/Helpers/ArrayHelper.php

@ -0,0 +1,42 @@
<?php
namespace RouterOS\Helpers;
/**
* Class ArrayHelper
*
* @package RouterOS\Helpers
* @since 0.7
*/
class ArrayHelper
{
/**
* Check if required keys in array
*
* @param array $keys
* @param array $array
* @return string|bool Return true if all fine, and string with name of key which was not found
*/
public static function checkIfKeysNotExist(array $keys, array $array)
{
$output = [];
foreach ($keys as $key) {
if (!array_key_exists($key, $array) && isset($array[$key])) {
$output[] = $key;
}
}
return !empty($output) ? implode(',', $output) : true;
}
/**
* Check if key in list of parameters
*
* @param string $key
* @param array $array
* @return bool
*/
public static function checkIfKeyNotExist(string $key, array $array): bool
{
return (!array_key_exists($key, $array));
}
}

25
src/Helpers/TypeHelper.php

@ -0,0 +1,25 @@
<?php
namespace RouterOS\Helpers;
/**
* Class TypeHelper
*
* @package RouterOS\Helpers
* @since 0.7
*/
class TypeHelper
{
/**
* 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
* @return bool
*/
public static function checkIfTypeMismatch(string $name, $whatType, $isType): bool
{
return ($whatType !== $isType);
}
}
Loading…
Cancel
Save