mirror of
https://github.com/predis/predis.git
synced 2026-08-30 04:02:22 +00:00
Simplify implementation of Predis\Connection\Parameters.
Casting values supplied by users should be done by the consumer since we cannot cover any possible use case anyway. It is still possible to subclass Predis\Connection\Parameters and override the filter() method to convert certain values if deemed necessary.
This commit is contained in:
@@ -29,14 +29,6 @@ class Parameters implements ParametersInterface
|
||||
'timeout' => 5.0,
|
||||
);
|
||||
|
||||
private static $casters = array(
|
||||
'port' => 'self::castInteger',
|
||||
'async_connect' => 'self::castBoolean',
|
||||
'persistent' => 'self::castBoolean',
|
||||
'timeout' => 'self::castFloat',
|
||||
'read_write_timeout' => 'self::castFloat',
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $parameters Named array of connection parameters.
|
||||
*/
|
||||
@@ -55,16 +47,6 @@ class Parameters implements ParametersInterface
|
||||
return self::$defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns cast functions for user-supplied parameter values.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getValueCasters()
|
||||
{
|
||||
return self::$casters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance by supplying the initial parameters either in the
|
||||
* form of an URI string or a named array.
|
||||
@@ -114,52 +96,11 @@ class Parameters implements ParametersInterface
|
||||
* @param array $parameters Connection parameters.
|
||||
* @return array
|
||||
*/
|
||||
private function filter(array $parameters)
|
||||
protected function filter(array $parameters)
|
||||
{
|
||||
if ($parameters) {
|
||||
$casters = array_intersect_key($this->getValueCasters(), $parameters);
|
||||
|
||||
foreach ($casters as $parameter => $caster) {
|
||||
$parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
|
||||
}
|
||||
}
|
||||
|
||||
return $parameters ?: array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates value as boolean.
|
||||
*
|
||||
* @param mixed $value Input value.
|
||||
* @return boolean
|
||||
*/
|
||||
private static function castBoolean($value)
|
||||
{
|
||||
return (bool) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates value as float.
|
||||
*
|
||||
* @param mixed $value Input value.
|
||||
* @return float
|
||||
*/
|
||||
private static function castFloat($value)
|
||||
{
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates value as integer.
|
||||
*
|
||||
* @param mixed $value Input value.
|
||||
* @return int
|
||||
*/
|
||||
private static function castInteger($value)
|
||||
{
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user