Skip to content

Commit

Permalink
Add optional argument to getPath method for building of real route pa…
Browse files Browse the repository at this point in the history
…th strings
  • Loading branch information
philipobenito committed May 18, 2020
1 parent 48002e7 commit 5ccce47
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,19 @@ public function setParentGroup(RouteGroup $group): self
/**
* Get the path
*
* @param array $replacements
*
* @return string
*/
public function getPath(): string
public function getPath(array $replacements = []): string
{
return $this->path;
$toReplace = [];

foreach ($replacements as $wildcard => $actual) {
$toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual;
}

return preg_replace(array_keys($toReplace), array_values($toReplace), $this->path);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,22 @@ public function process(
$middleware, $middleware
], $route->getMiddlewareStack());
}

/**
* Asserts that passing actual values to getPath replaces the wildcards.
*
* @return void
*/
public function testGetPathReplacesWildcards()
{
$route = new Route('GET', '/a/{wildcard}/and/{wildcardWithMatcher:uuid}', function () {
});

$path = $route->getPath([
'wildcard' => 'replaced-wildcard',
'wildcardWithMatcher' => 'replaced-wildcard-with-matcher',
]);

$this->assertSame('/a/replaced-wildcard/and/replaced-wildcard-with-matcher', $path);
}
}

0 comments on commit 5ccce47

Please sign in to comment.