Skip to content

Commit faff249

Browse files
author
Robin de Graaf
committed
Add some utility functions.
1 parent 893bf1d commit faff249

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Parable Routing
22

3+
## 0.2.2
4+
5+
_Changes_
6+
7+
- 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.
8+
- Added `Route::hasMetadataValues(): bool`.
9+
310
## 0.2.1
411

512
_Changes_

src/Route.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ public function getMetadata(): Metadata
166166
return $this->metadata;
167167
}
168168

169+
public function hasMetadataValues(): bool
170+
{
171+
return count($this->metadata->getAll()) > 0;
172+
}
173+
169174
public function getMetadataValue(string $name)
170175
{
171176
return $this->metadata->get($name);

src/Router.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ class Router
1616
*/
1717
protected $routeNames = [];
1818

19+
/**
20+
* @param string[] $httpMethods
21+
* @param mixed $callable
22+
*/
23+
public function add(
24+
array $httpMethods,
25+
string $name,
26+
string $url,
27+
$callable,
28+
array $metadata = []
29+
): void {
30+
$this->addRoute(
31+
new Route($httpMethods, $name, $url, $callable, $metadata)
32+
);
33+
}
34+
1935
public function addRoute(Route $route): void
2036
{
2137
foreach ($route->getHttpMethods() as $httpMethod) {

tests/RouteTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ function(string $p1, string $p2) {
9999
]));
100100
}
101101

102+
public function testNoMetadataIsHandledCorrectly()
103+
{
104+
$route = new Route(['GET'], 'name', 'url', function () {});
105+
106+
self::assertFalse($route->hasMetadataValues());
107+
self::assertEmpty($route->getMetadata()->getAll());
108+
}
109+
102110
public function testMetadataOnRouteCreation()
103111
{
104112
$route = new Route(
@@ -113,6 +121,7 @@ function() {
113121
]
114122
);
115123

124+
self::assertTrue($route->hasMetadataValues());
116125
self::assertInstanceOf(Metadata::class, $route->getMetadata());
117126
self::assertSame('yeah.phtml', $route->getMetadataValue('template'));
118127
}

tests/RouterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ public function testAddRouteAndGetRouteByName()
6464
self::assertNull($route->getCallable());
6565

6666
self::assertFalse($route->hasParameterValues());
67+
self::assertFalse($route->hasMetadataValues());
68+
}
69+
70+
public function testAddAndGetRouteByName()
71+
{
72+
$this->router->add(['GET'], 'name-of-route', 'url', function() { return 'ran'; }, ['metadata' => true]);
73+
74+
$route = $this->router->getRouteByName('name-of-route');
75+
76+
self::assertSame(['GET'], $route->getHttpMethods());
77+
self::assertSame('/url', $route->getUrl());
78+
self::assertIsCallable($route->getCallable());
79+
80+
self::assertFalse($route->hasParameterValues());
81+
self::assertTrue($route->hasMetadataValues());
82+
83+
self::assertSame(true, $route->getMetadata()->get('metadata'));
6784
}
6885

6986
public function testInvalidGetRouteByNameReturnsNull()

0 commit comments

Comments
 (0)