Skip to content

Commit

Permalink
Fix #68: Properly set examples for URL parameters with regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Sep 17, 2020
1 parent 5c9cef3 commit cf44fbb
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit cf44fbb

Please sign in to comment.