Browse Source

naming of throw methods changed

tags/0.6
Paul Rock 7 years ago
parent
commit
2297e50223
  1. 6
      src/Client.php
  2. 18
      src/Config.php

6
src/Client.php

@ -49,7 +49,7 @@ class Client implements Interfaces\ClientInterface
public function __construct(ConfigInterface $config) public function __construct(ConfigInterface $config)
{ {
// Check for important keys // Check for important keys
$this->keysCheck(['host', 'user', 'pass'], $config);
$this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config);
// Save config if everything is okay // Save config if everything is okay
$this->_config = $config; $this->_config = $config;
@ -67,11 +67,11 @@ class Client implements Interfaces\ClientInterface
* @param ConfigInterface $config * @param ConfigInterface $config
* @throws ConfigException * @throws ConfigException
*/ */
private function keysCheck(array $keys, ConfigInterface $config)
private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config)
{ {
$parameters = $config->getParameters(); $parameters = $config->getParameters();
foreach ($keys as $key) { foreach ($keys as $key) {
if (false === (array_key_exists($key, $parameters) && isset($parameters[$key]))) {
if (!array_key_exists($key, $parameters) && isset($parameters[$key])) {
throw new ConfigException("Parameter '$key' of Config is not set or empty"); throw new ConfigException("Parameter '$key' of Config is not set or empty");
} }
} }

18
src/Config.php

@ -25,16 +25,16 @@ class Config implements ConfigInterface
]; ];
/** /**
* Check if key in list of allowed parameters
* Check if key in list of parameters
* *
* @param string $key * @param string $key
* @param array $array * @param array $array
* @throws ConfigException * @throws ConfigException
*/ */
private function keyAllowed(string $key, array $array)
private function exceptionIfKeyNotExist(string $key, array $array)
{ {
if (!array_key_exists($key, $array)) { if (!array_key_exists($key, $array)) {
throw new ConfigException("Requested parameter '$key' not found in allowed list [" . implode(',',
throw new ConfigException("Requested parameter '$key' not found in list [" . implode(',',
array_keys($array)) . ']'); array_keys($array)) . ']');
} }
} }
@ -47,10 +47,10 @@ class Config implements ConfigInterface
* @param mixed $isType What type should be * @param mixed $isType What type should be
* @throws ConfigException * @throws ConfigException
*/ */
private function keyType(string $name, $whatType, $isType)
private function exceptionIfTypeMismatch(string $name, $whatType, $isType)
{ {
if ($whatType !== $isType) { if ($whatType !== $isType) {
throw new ConfigException("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
throw new ConfigException("Parameter '$name' has wrong type '$whatType' but should be '$isType'");
} }
} }
@ -65,10 +65,10 @@ class Config implements ConfigInterface
public function set(string $name, $value): ConfigInterface public function set(string $name, $value): ConfigInterface
{ {
// Check of key in array // Check of key in array
$this->keyAllowed($name, self::ALLOWED);
$this->exceptionIfKeyNotExist($name, self::ALLOWED);
// Check what type has this value // Check what type has this value
$this->keyType($name, \gettype($value), self::ALLOWED[$name]);
$this->exceptionIfTypeMismatch($name, \gettype($value), self::ALLOWED[$name]);
// Save value to array // Save value to array
$this->_parameters[$name] = $value; $this->_parameters[$name] = $value;
@ -104,7 +104,7 @@ class Config implements ConfigInterface
public function delete(string $parameter): ConfigInterface public function delete(string $parameter): ConfigInterface
{ {
// Check of key in array // Check of key in array
$this->keyAllowed($parameter, self::ALLOWED);
$this->exceptionIfKeyNotExist($parameter, self::ALLOWED);
// Save value to array // Save value to array
unset($this->_parameters[$parameter]); unset($this->_parameters[$parameter]);
@ -122,7 +122,7 @@ class Config implements ConfigInterface
public function get(string $parameter) public function get(string $parameter)
{ {
// Check of key in array // Check of key in array
$this->keyAllowed($parameter, self::ALLOWED);
$this->exceptionIfKeyNotExist($parameter, self::ALLOWED);
return $this->getPort($parameter) ?? $this->_parameters[$parameter]; return $this->getPort($parameter) ?? $this->_parameters[$parameter];
} }

Loading…
Cancel
Save