diff --git a/src/Path/DynamicPath.php b/src/Path/DynamicPath.php index 96b89b3..5072d9f 100644 --- a/src/Path/DynamicPath.php +++ b/src/Path/DynamicPath.php @@ -14,89 +14,58 @@ public function __construct( $this->assemblyList = $assemblyList; } - public function get(?string $key = null, bool $extra = false):?string { - $requestPathParts = explode("/", $this->requestPath); + /** + * With no key, return the deepest dynamic value across all assemblies. + * With a key, select its one-based occurrence, defaulting to the first. + * Return null if the requested occurrence is absent. + * Boolean indices retain the legacy positional extra argument. + */ + public function get(?string $key = null, int|bool $index = 1, bool $extra = false):?string { + if(is_bool($index)) { + $extra = $index; + $index = 1; + } + $matches = $this->getMatchingParts($key, $extra); + ksort($matches); + if(is_null($key)) { + return $matches ? end($matches) : null; + } + return array_values($matches)[$index - 1] ?? null; + } + /** @return array */ + private function getMatchingParts(?string $key, bool $extra):array { + $requestPathParts = explode("/", $this->requestPath); + $matches = []; foreach($this->assemblyList as $assembly) { foreach($assembly as $filePath) { - $filePathParts = explode("/", $filePath); - - foreach($filePathParts as $i => $f) { - $f = strtok($f, "."); - if($f[0] !== "@") { + foreach(explode("/", $filePath) as $i => $part) { + $placeholder = explode(".", $part)[0]; + if(!str_starts_with($placeholder, "@")) { continue; } - - if(is_null($key)) { - return $this->handleNullKey( - $filePathParts, - $requestPathParts, - $extra, - ); + if(!is_null($key) && ltrim($placeholder, "@") !== $key) { + continue; } - - if($this->matchesKey($f, $key)) { - $matchedKey = $this->getMatchedKey( - $requestPathParts, - $i, - ); - if(!$matchedKey) { - continue; - } - return $matchedKey; + $value = $this->getPartValue($requestPathParts, $i, $placeholder, $extra); + if(!is_null($value)) { + $matches[$i] = $value; } } } } - - return null; - } - - /** - * @param array $filePathParts - * @param array $requestPathParts - */ - private function handleNullKey( - array $filePathParts, - array $requestPathParts, - bool $extra, - ):?string { - if($extra) { - return $this->getExtraPath($filePathParts, $requestPathParts); - } - - return $requestPathParts[count($filePathParts) - 1] ?? null; - } - - /** - * @param array $filePathParts - * @param array $requestPathParts - */ - private function getExtraPath( - array $filePathParts, - array $requestPathParts, - ):string { - $test = ""; - for($ppi = count($filePathParts), $len = count($requestPathParts); $ppi < $len; $ppi++) { - $test .= $requestPathParts[$ppi] . "/"; - } - return rtrim($test, "/"); - } - - private function matchesKey(string $placeholder, string $key):bool { - return ltrim($placeholder, "@") === $key; + return $matches; } /** @param array $requestPathParts */ - private function getMatchedKey( - array $requestPathParts, - int $index, - ):?string { - $matchedKey = $requestPathParts[$index] ?? null; - if(!$matchedKey) { - return null; + private function getPartValue(array $requestPathParts, int $index, string $placeholder, bool $extra):?string { + if($extra) { + return str_starts_with($placeholder, "@@") + ? rtrim(implode("/", array_slice($requestPathParts, $index + 1)), "/") + : null; } - return $matchedKey; + $value = $requestPathParts[$index] ?? ""; + return $value === "" ? null : $value; } public function getUrl(string $viewBasePath):string { diff --git a/test/phpunit/Path/DynamicPathTest.php b/test/phpunit/Path/DynamicPathTest.php index 5f0e35a..488320d 100644 --- a/test/phpunit/Path/DynamicPathTest.php +++ b/test/phpunit/Path/DynamicPathTest.php @@ -6,161 +6,146 @@ use PHPUnit\Framework\TestCase; class DynamicPathTest extends TestCase { + public function testGet_deepestRepeatedKey():void { + $assembly = new Assembly(); + $assembly->add("page/groups/@slug/_common.php"); + $assembly->add("page/groups/@slug/events/@slug/index.php"); + $sut = new DynamicPath("/groups/my-group/events/my-event", $assembly); + self::assertSame("my-event", $sut->get()); + } + + public function testGet_deepestDistinctKeyAcrossAssemblies():void { + $view = new Assembly(); + $view->add("page/groups/@group/index.html"); + $logic = new Assembly(); + $logic->add("page/groups/@group/events/@event/index.php"); + $sut = new DynamicPath("/groups/my-group/events/my-event", $view, $logic); + self::assertSame("my-event", $sut->get()); + } + + public function testGet_repeatedKeyOccurrence():void { + $view = new Assembly(); + $view->add("page/groups/@slug/_header.html"); + $view->add("page/groups/@slug/events/@slug/index.html"); + $logic = new Assembly(); + $logic->add("page/groups/@slug/events/@slug/index.php"); + $sut = new DynamicPath("/groups/my-group/events/my-event", $view, $logic); + self::assertSame("my-group", $sut->get("slug")); + self::assertSame("my-group", $sut->get("slug", 1)); + self::assertSame("my-event", $sut->get("slug", 2)); + self::assertNull($sut->get("slug", 3)); + self::assertNull($sut->get("slug", 0)); + } + + public function testGet_extraArgumentsWithSharedParent():void { + $assembly = new Assembly(); + $assembly->add("page/shop/@category/_common.php"); + $assembly->add("page/shop/@category/@@item.php"); + $sut = new DynamicPath("/shop/phones/handset/64GB/refurbished", $assembly); + self::assertSame("64GB/refurbished", $sut->get(extra: true)); + self::assertSame("64GB/refurbished", $sut->get(null, true)); + self::assertSame("handset", $sut->get(null, false)); + self::assertSame("64GB/refurbished", (string)$sut->getExtra()); + } + + public function testGet_zeroValue():void { + $assembly = new Assembly(); + $assembly->add("page/@name/@name.php"); + $sut = new DynamicPath("/first/0", $assembly); + self::assertSame("0", $sut->get()); + self::assertSame("0", $sut->get("name", 2)); + } + public function testGet_noAssembly():void { $sut = new DynamicPath("/"); self::assertNull($sut->get("something")); } public function testGet_withAssembly_noMatch():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/@dynamic/something.html", - "page/@dynamic/something-else.html" - ); - $assembly->method("valid") - ->willReturn( - true, - true, - false - ); + $assembly = new Assembly(); + $assembly->add("page/@dynamic/something.html"); + $assembly->add("page/@dynamic/something-else.html"); $sut = new DynamicPath("/", $assembly); self::assertNull($sut->get("dynamic")); } public function testGet_byKey():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/shop/_common.php", - "page/shop/@category/@itemName.php", - "page/shop/_common.php", - "page/shop/@category/@itemName.php", - ); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/_common.php"); + $assembly->add("page/shop/@category/@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T", $assembly); self::assertEquals("OnePlus", $sut->get("category")); self::assertEquals("6T", $sut->get("itemName")); } public function testGet_noKey_shouldReturnDeepest():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/shop/_common.php", - "page/shop/_common.php", - "page/shop/@category/@itemName.php", - ); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/_common.php"); + $assembly->add("page/shop/@category/@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T", $assembly); self::assertEquals("6T", $sut->get()); } public function testGet_noKey_returnsRightMostDynamicPathPartAcrossMatches():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/shop/@category/index.php", - "page/shop/@category/@itemName.php", - ); - $assembly->method("valid") - ->willReturn( - true, - true, - false, - ); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/index.php"); + $assembly->add("page/shop/@category/@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T", $assembly); self::assertSame("6T", $sut->get()); } public function testGet_noKey_returnsRightMostStandardDynamicPartBeforeExtraPart():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/shop/@category/index.php", - "page/shop/@category/@@itemName.php", - ); - $assembly->method("valid") - ->willReturn( - true, - true, - false, - ); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/index.php"); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T/64GB", $assembly); self::assertSame("6T", $sut->get()); } public function testGetUrl():void { - $viewAssembly = self::createMock(Assembly::class); - $viewAssembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/_header.html", - "page/_footer.html", - "page/shop/@category/@itemName.html", - ); - $viewAssembly->method("valid") - ->willReturn(true); - $logicAssembly = self::createMock(Assembly::class); - $logicAssembly->method("current") - ->willReturnOnConsecutiveCalls( - "page/_common.php", - "page/shop/_common.php", - "page/shop/@category/@itemName.php", - ); - $logicAssembly->method("valid") - ->willReturn(true); + $viewAssembly = new Assembly(); + $viewAssembly->add("page/_header.html"); + $viewAssembly->add("page/_footer.html"); + $viewAssembly->add("page/shop/@category/@itemName.html"); + $logicAssembly = new Assembly(); + $logicAssembly->add("page/_common.php"); + $logicAssembly->add("page/shop/_common.php"); + $logicAssembly->add("page/shop/@category/@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T", $viewAssembly, $logicAssembly); self::assertSame("/shop/@category/@itemName", $sut->getUrl("page/")); } public function testDeeperDynamicPages_noExtra():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturn("page/shop/@category/@@itemName.php"); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T", $assembly); self::assertEquals("6T", $sut->get()); } public function testDeeperDynamicPages_getByName():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturn("page/shop/@category/@@itemName.php"); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T/64GB", $assembly); self::assertEquals("6T", $sut->get("itemName")); } public function testDeeperDynamicPages_getExtra():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturn("page/shop/@category/@@itemName.php"); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T/64GB", $assembly); self::assertEquals("64GB", $sut->getExtra()); } public function testDeeperDynamicPages_getExtraMultiple():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturn("page/shop/@category/@@itemName.php"); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T/64GB/refurbished", $assembly); self::assertEquals("64GB/refurbished", $sut->getExtra()); } public function testDeeperDynamicPages_getExtraMultiple_indexed():void { - $assembly = self::createMock(Assembly::class); - $assembly->method("current") - ->willReturn("page/shop/@category/@@itemName.php"); - $assembly->method("valid") - ->willReturn(true); + $assembly = new Assembly(); + $assembly->add("page/shop/@category/@@itemName.php"); $sut = new DynamicPath("/shop/OnePlus/6T/64GB/refurbished", $assembly); self::assertEquals("64GB", $sut->getExtra()->getIndex(0)); }