Merge pull request #202 from skipperbent/v2-development

`getUrl` will now use default-parameter value if specifically set to null when using `url()`.
This commit is contained in:
Simon Sessingø
2016-11-28 14:00:59 +01:00
committed by GitHub
+29 -26
View File
@@ -107,47 +107,50 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/ */
public function findUrl($method = null, $parameters = null, $name = null) public function findUrl($method = null, $parameters = null, $name = null)
{ {
$url = ''; $url = $this->getUrl();
$parameters = (array)$parameters;
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) { if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
$url .= '//' . $this->getGroup()->getDomains()[0]; $url = '//' . $this->getGroup()->getDomains()[0] . $url;
} }
$url .= $this->getUrl(); if($parameters !== null) {
$params = array_merge($this->getParameters(), $parameters); $params = array_merge($this->getParameters(), $parameters);
/* Url that contains parameters that aren't recognized */ /* Contains parameters that aren't recognized and will be appended at the end of the url */
$unknownParams = []; $unknownParams = [];
/* Create the param string - {} */ /* Create the param string - {parameter} */
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1]; $param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
/* Create the param string with the optional symbol - {?} */ /* Create the param string with the optional symbol - {parameter?} */
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1]; $param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
/* Let's parse the values of any {} parameter in the url */ /* Replace any {parameter} in the url with the correct value */
$max = count($params) - 1; $max = count($params) - 1;
$keys = array_keys($params); $keys = array_keys($params);
for ($i = $max; $i >= 0; $i--) { for ($i = $max; $i >= 0; $i--) {
$param = $keys[$i]; $param = $keys[$i];
$value = $params[$param]; $value = $value = isset($parameters[$param]) ? $parameters[$param] : $params[$param];
$value = isset($parameters[$param]) ? $parameters[$param] : $value; /* If parameter is specifically set to null - use the original-defined value */
if (array_key_exists($param, $parameters) && $parameters[$param] === null && isset($this->originalParameters[$param])) {
$value = $this->originalParameters[$param];
}
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) { if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url); /* Add parameter to the correct position */
} else { $url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
$unknownParams[$param] = $value; } else {
$unknownParams[$param] = $value;
}
} }
}
/** @noinspection AliasFunctionsUsageInspection */ $url .= join('/', $unknownParams);
$url .= join('/', $unknownParams);
}
return rtrim($url, '/') . '/'; return rtrim($url, '/') . '/';
} }