Bladeren bron

Fix #68: Properly set examples for URL parameters with regexes

shalvah 4 jaren geleden
bovenliggende
commit
cf44fbbcd3
1 gewijzigde bestanden met toevoegingen van 4 en 8 verwijderingen
  1. 4 8
      src/Tools/Utils.php

+ 4 - 8
src/Tools/Utils.php

@@ -58,18 +58,14 @@ class Utils
      */
     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'