Skip to content

Commit 7b5080b

Browse files
author
Greg Bowler
authored
Merge pull request #6 from PhpGt/5-dynamic-get
feature: dynamic path without a get parameter returns deepest dynamic match
2 parents fc0eaf1 + 83053cf commit 7b5080b

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

example/01-test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// Request 1: A page request, as if it is sent from a web browser.
3232
$pageRequest = new Request(
3333
"GET",
34-
new Uri("/shop/furniture/chair"),
34+
new Uri("/shop/phone/oneplus"),
3535
new RequestHeaders([
3636
// An example accept header from Firefox when requesting a normal link:
3737
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"

example/project/simple-site/page/explorer/topic/@id.html

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
function go(\Gt\Routing\Path\DynamicPath $path) {
3+
echo "The topic is: " . $path->get("id"), PHP_EOL;
4+
}

src/Path/DynamicPath.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(
1414
$this->assemblyList = $assemblyList;
1515
}
1616

17-
public function get(string $key):?string {
17+
public function get(string $key = null):?string {
1818
$requestPathParts = explode("/", $this->requestPath);
1919

2020
foreach($this->assemblyList as $assembly) {
@@ -25,6 +25,11 @@ public function get(string $key):?string {
2525
if($f[0] !== "@") {
2626
continue;
2727
}
28+
29+
if(is_null($key)) {
30+
return $requestPathParts[count($filePathParts) - 1] ?? null;
31+
}
32+
2833
if("@$key" !== $f) {
2934
continue;
3035
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace Gt\Routing\Test\Path;
3+
4+
use Gt\Routing\Assembly;
5+
use Gt\Routing\Path\DynamicPath;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DynamicPathTest extends TestCase {
9+
public function testGet_noAssembly():void {
10+
$sut = new DynamicPath("/");
11+
self::assertNull($sut->get("something"));
12+
}
13+
14+
public function testGet_withAssembly_noMatch():void {
15+
$assembly = self::createMock(Assembly::class);
16+
$assembly->method("current")
17+
->willReturnOnConsecutiveCalls(
18+
"page/@dynamic/something.html",
19+
"page/@dynamic/something-else.html"
20+
);
21+
$assembly->method("valid")
22+
->willReturn(
23+
true,
24+
true,
25+
false
26+
);
27+
$sut = new DynamicPath("/", $assembly);
28+
self::assertNull($sut->get("dynamic"));
29+
}
30+
31+
public function testGet_byKey():void {
32+
$assembly = self::createMock(Assembly::class);
33+
$assembly->method("current")
34+
->willReturnOnConsecutiveCalls(
35+
"page/shop/_common.php",
36+
"page/shop/@category/@itemName.php",
37+
"page/shop/_common.php",
38+
"page/shop/@category/@itemName.php",
39+
);
40+
$assembly->method("valid")
41+
->willReturn(true);
42+
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
43+
self::assertEquals("OnePlus", $sut->get("category"));
44+
self::assertEquals("6T", $sut->get("itemName"));
45+
}
46+
47+
public function testGet_noKey_shouldReturnDeepest():void {
48+
$assembly = self::createMock(Assembly::class);
49+
$assembly->method("current")
50+
->willReturnOnConsecutiveCalls(
51+
"page/shop/_common.php",
52+
"page/shop/@category/@itemName.php",
53+
"page/shop/_common.php",
54+
"page/shop/@category/@itemName.php",
55+
);
56+
$assembly->method("valid")
57+
->willReturn(true);
58+
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
59+
self::assertEquals("6T", $sut->get());
60+
}
61+
}

0 commit comments

Comments
 (0)