From cf44fbbcd3643086859ba724f6e4d4315941b471 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 17 Sep 2020 22:29:40 +0100 Subject: [PATCH] Fix #68: Properly set examples for URL parameters with regexes --- src/Tools/Utils.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) 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'