Skip to content

Commit

Permalink
Add some utility functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Apr 13, 2019
1 parent 893bf1d commit faff249
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
5 changes: 5 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -113,6 +121,7 @@ function() {
]
);

self::assertTrue($route->hasMetadataValues());
self::assertInstanceOf(Metadata::class, $route->getMetadata());
self::assertSame('yeah.phtml', $route->getMetadataValue('template'));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit faff249

Please sign in to comment.