Skip to content

Commit

Permalink
RouterInterface::generate() accepts a RouteInterface object or st…
Browse files Browse the repository at this point in the history
…ring instead of only string
  • Loading branch information
ElGigi committed Nov 4, 2024
1 parent bc055f4 commit 3f4c502
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
9 changes: 8 additions & 1 deletion tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand All @@ -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(
Expand Down

0 comments on commit 3f4c502

Please sign in to comment.