Skip to content

Commit 5ccce47

Browse files
committed
Add optional argument to getPath method for building of real route path strings
1 parent 48002e7 commit 5ccce47

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Route.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,19 @@ public function setParentGroup(RouteGroup $group): self
180180
/**
181181
* Get the path
182182
*
183+
* @param array $replacements
184+
*
183185
* @return string
184186
*/
185-
public function getPath(): string
187+
public function getPath(array $replacements = []): string
186188
{
187-
return $this->path;
189+
$toReplace = [];
190+
191+
foreach ($replacements as $wildcard => $actual) {
192+
$toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual;
193+
}
194+
195+
return preg_replace(array_keys($toReplace), array_values($toReplace), $this->path);
188196
}
189197

190198
/**

tests/RouteTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,22 @@ public function process(
167167
$middleware, $middleware
168168
], $route->getMiddlewareStack());
169169
}
170+
171+
/**
172+
* Asserts that passing actual values to getPath replaces the wildcards.
173+
*
174+
* @return void
175+
*/
176+
public function testGetPathReplacesWildcards()
177+
{
178+
$route = new Route('GET', '/a/{wildcard}/and/{wildcardWithMatcher:uuid}', function () {
179+
});
180+
181+
$path = $route->getPath([
182+
'wildcard' => 'replaced-wildcard',
183+
'wildcardWithMatcher' => 'replaced-wildcard-with-matcher',
184+
]);
185+
186+
$this->assertSame('/a/replaced-wildcard/and/replaced-wildcard-with-matcher', $path);
187+
}
170188
}

0 commit comments

Comments
 (0)