diff --git a/src/Tools/Utils.php b/src/Tools/Utils.php index 06c10b90..06c2047a 100644 --- a/src/Tools/Utils.php +++ b/src/Tools/Utils.php @@ -58,18 +58,14 @@ public static function getRouteClassAndMethodNames(Route $route): array */ public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters) { - $matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths); - if (!$matches) { + if (empty($urlParameters)) { return $uri; } - foreach ($parameterPaths[0] as $parameterPath) { - $key = trim($parameterPath, '{?}'); - if (isset($urlParameters[$key])) { - $example = $urlParameters[$key]; - $uri = str_replace($parameterPath, $example, $uri); - } + foreach ($urlParameters as $parameterName => $example) { + $uri = preg_replace('#\{' . $parameterName . '[^/]*?}?}#', $example, $uri); // The second closing brace is present to account for regexes ending in a } } + // Remove unbound optional parameters with nothing $uri = preg_replace('#{([^/]+\?)}#', '', $uri); // Replace any unbound non-optional parameters with '1'