Web/Url: more intuitive separator usage

One might not expect to get a URL with params separated by & while
this is the correct way of writing them to HTML attributes. So default
behaviour is right now that all functions return URL params separated
by &, but if you cast a URL (or it's params) to a string, & will be
used.

Additionally there are toString functions allowing you to choose the
separator "manually".

refs #6699
This commit is contained in:
Thomas Gelf 2014-08-19 09:42:16 +02:00
parent 7caccd7691
commit 217595dc15
2 changed files with 15 additions and 7 deletions

View File

@ -207,12 +207,12 @@ class Url
*
* @return string
*/
public function getRelativeUrl()
public function getRelativeUrl($separator = '&')
{
if ($this->params->isEmpty()) {
return $this->path . $this->anchor;
} else {
return $this->path . '?' . $this->params->setSeparator('&') . $this->anchor;
return $this->path . '?' . $this->params->toString($separator) . $this->anchor;
}
}
@ -232,9 +232,9 @@ class Url
*
* @return string
*/
public function getAbsoluteUrl()
public function getAbsoluteUrl($separator = '&')
{
return $this->baseUrl . ($this->baseUrl !== '/' ? '/' : '') . $this->getRelativeUrl();
return $this->baseUrl . ($this->baseUrl !== '/' ? '/' : '') . $this->getRelativeUrl($separator);
}
/**
@ -416,6 +416,6 @@ class Url
*/
public function __toString()
{
return $this->getAbsoluteUrl();
return $this->getAbsoluteUrl('&');
}
}

View File

@ -315,8 +315,11 @@ class UrlParams
return $this->params;
}
public function __toString()
public function toString($separator = null)
{
if ($separator === null) {
$separator = $this->separator;
}
$parts = array();
foreach ($this->params as $p) {
if ($p[1] === true) {
@ -325,7 +328,12 @@ class UrlParams
$parts[] = $p[0] . '=' . $p[1];
}
}
return implode($this->separator, $parts);
return implode($separator, $parts);
}
public function __toString()
{
return $this->toString();
}
public static function fromQueryString($queryString = null)