diff --git a/CHANGELOG.md b/CHANGELOG.md index e2bac51..62f1e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.4.0] - 2024-11-04 + +### Changed + +- `RouterInterface::generate()` accepts a `RouteInterface` object or string instead of only string + ## [2.3.0] - 2024-11-04 ### Added diff --git a/src/Router.php b/src/Router.php index f9fbe41..b08f28e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -89,19 +89,20 @@ protected function log(string $level, string $message): void /** * Generate route. * - * @param string $name + * @param string|RouteInterface $route * @param array|RouteAttributes $parameters * * @return string + * @throws NotFoundException * @throws RoutingException */ - public function generate(string $name, array|RouteAttributes $parameters = []): string + public function generate(string|RouteInterface $route, array|RouteAttributes $parameters = []): string { $parameters = $this->generateParameters($parameters); - $route = $this->getRoute($name); + is_string($route) && $route = $this->getRoute($route); if (null === $route) { - throw new NotFoundException(sprintf('Route "%s" does not exists', $name)); + throw new NotFoundException(sprintf('Route "%s" does not exists', $route)); } $str = $route->generate($parameters); diff --git a/src/RouterInterface.php b/src/RouterInterface.php index f643e44..6ff9c87 100644 --- a/src/RouterInterface.php +++ b/src/RouterInterface.php @@ -27,13 +27,13 @@ interface RouterInterface extends RouteSetInterface /** * Generate route. * - * @param string $name + * @param string|RouteInterface $route * @param array|RouteAttributes $parameters * * @return string * @throws RoutingException */ - public function generate(string $name, array|RouteAttributes $parameters = []): string; + public function generate(string|RouteInterface $route, array|RouteAttributes $parameters = []): string; /** * Is valid request? diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 127bc23..a032b82 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -50,7 +50,7 @@ public function testGetRoutes() public function testGenerate() { $router = new Router; - $router->addRoute(new Route('/path/{attr1}/sub-path', name: 'route1')); + $router->addRoute($route1 = new Route('/path/{attr1}/sub-path', name: 'route1')); $router->addRoute( new Route( '/path/{attr1}/sub-path/{attr2}', @@ -66,6 +66,13 @@ public function testGenerate() ['attr1' => 'test'] ) ); + $this->assertEquals( + '/path/test/sub-path', + $router->generate( + $route1, + ['attr1' => 'test'] + ) + ); $this->assertEquals( '/path/test/sub-path/test2', $router->generate(