diff --git a/flight/net/Route.php b/flight/net/Route.php index 4ff28d7..2d14bd2 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -178,7 +178,7 @@ final class Route * @return string */ public function hydrateUrl(array $params = []): string { - $url = preg_replace_callback("/(?:@([a-zA-Z]+)(?:\:([^\/]+))?\)*)/i", function($match) use ($params) { + $url = preg_replace_callback("/(?:@([a-zA-Z0-9]+)(?:\:([^\/]+))?\)*)/i", function($match) use ($params) { if(isset($match[1]) && isset($params[$match[1]])) { return $params[$match[1]]; } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index c827302..42e3781 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -500,18 +500,36 @@ class RouterTest extends PHPUnit\Framework\TestCase $this->assertEquals('/path1/123', $url); } + public function testGetUrlByAliasSimpleParamsWithNumber() { + $this->router->map('/path1/@id1', [$this, 'ok'], false, 'path1'); + $url = $this->router->getUrlByAlias('path1', ['id1' => 123]); + $this->assertEquals('/path1/123', $url); + } + public function testGetUrlByAliasSimpleOptionalParamsWithParam() { $this->router->map('/path1(/@id)', [$this, 'ok'], false, 'path1'); $url = $this->router->getUrlByAlias('path1', ['id' => 123]); $this->assertEquals('/path1/123', $url); } + public function testGetUrlByAliasSimpleOptionalParamsWithNumberWithParam() { + $this->router->map('/path1(/@id1)', [$this, 'ok'], false, 'path1'); + $url = $this->router->getUrlByAlias('path1', ['id1' => 123]); + $this->assertEquals('/path1/123', $url); + } + public function testGetUrlByAliasSimpleOptionalParamsNoParam() { $this->router->map('/path1(/@id)', [$this, 'ok'], false, 'path1'); $url = $this->router->getUrlByAlias('path1'); $this->assertEquals('/path1', $url); } + public function testGetUrlByAliasSimpleOptionalParamsWithNumberNoParam() { + $this->router->map('/path1(/@id1)', [$this, 'ok'], false, 'path1'); + $url = $this->router->getUrlByAlias('path1'); + $this->assertEquals('/path1', $url); + } + public function testGetUrlByAliasMultipleParams() { $this->router->map('/path1/@id/@name', [$this, 'ok'], false, 'path1'); $url = $this->router->getUrlByAlias('path1', ['id' => 123, 'name' => 'abc']); @@ -524,6 +542,12 @@ class RouterTest extends PHPUnit\Framework\TestCase $this->assertEquals('/path1/123/abc', $url); } + public function testGetUrlByAliasMultipleComplexParamsWithNumbers() { + $this->router->map('/path1/@5id:[0-9]+/@n1ame:[a-zA-Z0-9]{5}', [$this, 'ok'], false, 'path1'); + $url = $this->router->getUrlByAlias('path1', ['5id' => '123', 'n1ame' => 'abc']); + $this->assertEquals('/path1/123/abc', $url); + } + public function testGetUrlByAliasMultipleComplexOptionalParamsMissingOne() { $this->router->map('/path1(/@id:[0-9]+(/@name(/@crazy:[a-z]{5})))', [$this, 'ok'], false, 'path1'); $url = $this->router->getUrlByAlias('path1', ['id' => '123', 'name' => 'abc']);