Skip to content

Commit

Permalink
Merge 840e8d3 into 1ca894d
Browse files Browse the repository at this point in the history
  • Loading branch information
luminateonedev committed Jul 23, 2021
2 parents 1ca894d + 840e8d3 commit dd14b34
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace League\Route;

use FastRoute\RouteParser\Std;
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait, StrategyInterface};
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -90,15 +91,34 @@ public function getParentGroup(): ?RouteGroup
return $this->group;
}

public function getPath(array $replacements = []): string
public function resolvePath(array $replacements = []): string
{
$toReplace = [];
$parser = new Std();
$routeData = $parser->parse($this->path);
$longestPossibleRoute = end($routeData);
$result = [];
foreach ($longestPossibleRoute as $routeSegment) {
if (is_string($routeSegment)) {
$result[] = $routeSegment;
} else if (is_array($routeSegment)) {
$wildcard = $routeSegment[0];
if (array_key_exists($wildcard, $replacements)) {
$result[] = $replacements[$wildcard];
} else {
break;
}
}
}
return rtrim(implode($result), '/');
}

foreach ($replacements as $wildcard => $actual) {
$toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual;
public function getPath(?array $replacements = null): string
{
if ($replacements !== null) {
return $this->resolvePath($replacements);
}

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

public function getVars(): array
Expand Down
58 changes: 58 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,64 @@ public function testGetPathReplacesWildcards(): void
$this->assertSame('/a/replaced-wildcard/and/replaced-wildcard-with-matcher', $path);
}

public function testGetPathReplacesOptional(): void
{
$route = new Route('GET', '/date[/{year:int}]', static function () {
});

$path = $route->getPath([
'year' => '2000'
]);

$this->assertSame('/date/2000', $path);
}

public function testGetPathReplacesMissing(): void
{
$route = new Route('GET', '/date[/{year:int}]', static function () {
});

$path = $route->getPath([]);

$this->assertSame('/date', $path);
}

public function testGetPathReplacesMissingMultiple(): void
{
$route = new Route('GET', '/date[/{year}[/{month}]]', static function () {
});

$path = $route->getPath([
'year' => '2000'
]);

$this->assertSame('/date/2000', $path);
}

public function testGetPathReplacesOptionalNested(): void
{
$route = new Route('GET', '/date[/{year}[/{month}[/{day}]]]', static function () {
});

$path = $route->getPath([
'year' => '2000',
'month' => '12',
'day' => '1',
]);

$this->assertSame('/date/2000/12/1', $path);
}

public function testGetPathReplacesOptionalNestedMissing(): void
{
$route = new Route('GET', '/date[/{year}[/{month}[/{day}]]]', static function () {
});

$path = $route->getPath([]);

$this->assertSame('/date', $path);
}

public function testRouteThrowsWithNoStrategy(): void
{
$this->expectException(RuntimeException::class);
Expand Down

0 comments on commit dd14b34

Please sign in to comment.