From 74f6f9ab85d3257e6d2d5ecc90f511cd1238f305 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 3 Apr 2011 09:26:53 +0200 Subject: [PATCH] Small refactoring of the internals of Predis\ConnectionParameters. --- lib/Predis/ConnectionParameters.php | 39 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/Predis/ConnectionParameters.php b/lib/Predis/ConnectionParameters.php index 137f0bf1..288e0f4a 100644 --- a/lib/Predis/ConnectionParameters.php +++ b/lib/Predis/ConnectionParameters.php @@ -93,28 +93,35 @@ class ConnectionParameters implements IConnectionParameters { return in_array($parameter, $this->_userDefined); } - public function __toString() { - $str = null; + protected function getBaseURI() { if ($this->scheme === 'unix') { - $str = "{$this->scheme}://{$this->path}"; - } - else { - $str = "{$this->scheme}://{$this->host}:{$this->port}"; + return "{$this->scheme}://{$this->path}"; } + return "{$this->scheme}://{$this->host}:{$this->port}"; + } - $query = array(); - $reject = array('scheme', 'host', 'port', 'password', 'path'); - foreach ($this->_userDefined as $k) { - if (in_array($k, $reject) || !isset($this->_parameters[$k])) { - continue; - } - $v = $this->_parameters[$k]; - $query[] = $k . '=' . ($v === false ? '0' : $v); - } - return count($query) > 0 ? ($str . '/?' . implode('&', $query)) : $str; + protected function getDisallowedURIParts() { + return array('scheme', 'host', 'port', 'password', 'path'); } public function toArray() { return $this->_parameters; } + + public function __toString() { + $query = array(); + $parameters = $this->toArray(); + $reject = $this->getDisallowedURIParts(); + foreach ($this->_userDefined as $param) { + if (in_array($param, $reject) || !isset($parameters[$param])) { + continue; + } + $value = $parameters[$param]; + $query[] = "$param=" . ($value === false ? '0' : $value); + } + if (count($query) === 0) { + return $this->getBaseURI(); + } + return $this->getBaseURI() . '/?' . implode('&', $query); + } }