Skip to content

Commit

Permalink
Fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonsalas committed Sep 27, 2018
1 parent 2d899d4 commit 145ef60
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,17 @@ public function hasParam($name)
}

/**
* Build route absolute url
* Builds the route absolute url
*
* @param string|array $params Route parameters
* @param mixed $params Route parameters
*
* @return string
*/
public function buildUrl($params)
{
$defaults = RouteBuilder::getDefaultParams();

// Thanks for @Ihabafia for the suggest!
// Thanks to @Ihabafia for the suggest!
if(is_object($params)){
$params = (array) $params;
}
Expand All @@ -392,16 +392,19 @@ public function buildUrl($params)
}

$path = $this->getPrefix() . '/' . $this->getPath();

$skippedOptional = null;

foreach($this->params as &$param)
{
$name = $param->getName();

if(!$param->isOptional() && !isset($defaults[$name]) && !isset($params[$param->getName()]))
$isMissingRequiredField = !$param->isOptional() && !isset($defaults[$name]) && !isset($params[$param->getName()]);
$alreadySkippedOptionalField = $param->isOptional() && $skippedOptional !== null && (isset($defaults[$name]) || isset($params[$name]));

if( $isMissingRequiredField || $alreadySkippedOptionalField )
{
throw new \Exception('Missing "' . $name .'" parameter for "' . $this->getName() . '" route');
throw new \Exception('Missing "' . ($skippedOptional === null ? $name : $skippedOptional) . '" parameter for "' . $this->getName() . '" route');
}

if(isset($defaults[$name]))
{
$param->value = $defaults[$param->getName()];
Expand All @@ -418,10 +421,13 @@ public function buildUrl($params)
}
else
{
$skippedOptional = $skippedOptional === null
? $param->getName()
: $skippedOptional;

$_path = explode('/', $path);
array_pop($_path);
$path = implode('/', $_path);
array_pop($this->params);
unset($_path[array_search($param->getSegment(), $_path)]);
$path = implode('/', $_path);
}
}

Expand Down

0 comments on commit 145ef60

Please sign in to comment.