diff --git a/CHANGELOG.md b/CHANGELOG.md index a1b555b..c33266c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Parable Routing +## 0.2.2 + +_Changes_ + +- Added `Router::add($httpMethods, $name, $url, $callable, $metadata)`, which does the same as `addRoute()` but will create a new `Route` instance for you. Utility function, but nice. +- Added `Route::hasMetadataValues(): bool`. + ## 0.2.1 _Changes_ diff --git a/src/Route.php b/src/Route.php index 3efdc9e..574aa36 100644 --- a/src/Route.php +++ b/src/Route.php @@ -166,6 +166,11 @@ public function getMetadata(): Metadata return $this->metadata; } + public function hasMetadataValues(): bool + { + return count($this->metadata->getAll()) > 0; + } + public function getMetadataValue(string $name) { return $this->metadata->get($name); diff --git a/src/Router.php b/src/Router.php index b55497f..3755ea6 100644 --- a/src/Router.php +++ b/src/Router.php @@ -16,6 +16,22 @@ class Router */ protected $routeNames = []; + /** + * @param string[] $httpMethods + * @param mixed $callable + */ + public function add( + array $httpMethods, + string $name, + string $url, + $callable, + array $metadata = [] + ): void { + $this->addRoute( + new Route($httpMethods, $name, $url, $callable, $metadata) + ); + } + public function addRoute(Route $route): void { foreach ($route->getHttpMethods() as $httpMethod) { diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 84322d6..4f8007f 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -99,6 +99,14 @@ function(string $p1, string $p2) { ])); } + public function testNoMetadataIsHandledCorrectly() + { + $route = new Route(['GET'], 'name', 'url', function () {}); + + self::assertFalse($route->hasMetadataValues()); + self::assertEmpty($route->getMetadata()->getAll()); + } + public function testMetadataOnRouteCreation() { $route = new Route( @@ -113,6 +121,7 @@ function() { ] ); + self::assertTrue($route->hasMetadataValues()); self::assertInstanceOf(Metadata::class, $route->getMetadata()); self::assertSame('yeah.phtml', $route->getMetadataValue('template')); } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 9471ca1..08882dd 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -64,6 +64,23 @@ public function testAddRouteAndGetRouteByName() self::assertNull($route->getCallable()); self::assertFalse($route->hasParameterValues()); + self::assertFalse($route->hasMetadataValues()); + } + + public function testAddAndGetRouteByName() + { + $this->router->add(['GET'], 'name-of-route', 'url', function() { return 'ran'; }, ['metadata' => true]); + + $route = $this->router->getRouteByName('name-of-route'); + + self::assertSame(['GET'], $route->getHttpMethods()); + self::assertSame('/url', $route->getUrl()); + self::assertIsCallable($route->getCallable()); + + self::assertFalse($route->hasParameterValues()); + self::assertTrue($route->hasMetadataValues()); + + self::assertSame(true, $route->getMetadata()->get('metadata')); } public function testInvalidGetRouteByNameReturnsNull()