summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
committerSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
commit11cb664b7a1956ec18227088be08b098a1a14865 (patch)
treeef83419e41dee549db9bae47a3e4aa6f530f3b1c
parenta61bcc830dfe801db7f94e01bbbe5a0b7b0a7dea (diff)
made and tested getFullPath and written getFullName.
-rw-r--r--src/Group.php34
-rw-r--r--tests/Unit/GroupTest.php27
2 files changed, 38 insertions, 23 deletions
diff --git a/src/Group.php b/src/Group.php
index c208180..9f08d25 100644
--- a/src/Group.php
+++ b/src/Group.php
@@ -38,18 +38,27 @@ class Group
return $this->prefix;
}
- public function getPath(): string
+ /**
+ * @param callable(Group): ?string $getter
+ *
+ * @return string[]
+ */
+ private function getAncestorString(callable $getter): array
{
$ancestors = $this->getAncestorsAndSelf();
- return '/' . implode('/', array_filter(
- array_map(fn(Group $g): ?string => (
- ($v = $g->getPrefix()) === null ?
- null :
- trim($v, '/')
- ), $ancestors),
- fn (?string $v) => $v !== null
- ));
+ return array_filter(
+ array_map(fn ($g) => $getter($g), $ancestors),
+ fn (?string $v): bool => null !== $v
+ );
+ }
+
+ public function getFullPrefix(): string
+ {
+ $prefixes = $this->getAncestorString(fn ($g) => $g->getPrefix());
+ $prefixes = array_map(fn (string $p) => trim($p, '/'), $prefixes);
+
+ return '/'.implode('/', $prefixes);
}
public function name(string $value): static
@@ -64,6 +73,13 @@ class Group
return $this->name;
}
+ public function getFullName(): ?string
+ {
+ $names = $this->getAncestorString(fn ($g) => $g->getName());
+
+ return 0 === count($names) ? null : implode('', $names);
+ }
+
/** @param callable(GroupDefinition): void $cb */
public function group(callable $cb): void
{
diff --git a/tests/Unit/GroupTest.php b/tests/Unit/GroupTest.php
index 6549295..a2f06b2 100644
--- a/tests/Unit/GroupTest.php
+++ b/tests/Unit/GroupTest.php
@@ -7,32 +7,32 @@ use Lightscale\Router\GroupDefinition;
use Lightscale\Router\Router;
use Lightscale\Router\Test\Utils\TestCallable;
-$make = fn(?Group $parent = null) => new Group(new Router, $parent);
+$make = fn (?Group $parent = null) => new Group(new Router(), $parent);
it('initializes with null defaults')
- ->expect(fn() => $make())
+ ->expect(fn () => $make())
->toBeInstanceOf(Group::class)
->getName()->toBeNull()
->getPrefix()->toBeNull();
it('initializes with values')
- ->expect(new Group(new Router, null, '/test-path', 'test-name'))
+ ->expect(new Group(new Router(), null, '/test-path', 'test-name'))
->toBeInstanceOf(Group::class)
->getName()->toBe('test-name')
->getPrefix()->toBe('/test-path');
it('can set/get prefix')
- ->expect(fn() => $make()->prefix('/test'))
+ ->expect(fn () => $make()->prefix('/test'))
->toBeInstanceOf(Group::class)
->getPrefix()->toBe('/test');
it('can set/get name')
- ->expect(fn() => $make()->name('test'))
+ ->expect(fn () => $make()->name('test'))
->toBeInstanceOf(Group::class)
->getName()->toBe('test');
it('calls group call back with definition', function () use ($make) {
- $cb = TestCallable::make(fn() => null);
+ $cb = TestCallable::make(fn () => null);
$make()->group($cb);
$cb->assertCalled();
@@ -55,7 +55,7 @@ it('can get all ancestors')
->toContainOnlyInstancesOf(Group::class)
->toHaveCount(2);
-it('order all ancestors root first', function () use($make) {
+it('order all ancestors root first', function () use ($make) {
$g3 = $make($g2 = $make($g1 = $make()));
expect($g3->getAncestors())
@@ -70,7 +70,7 @@ it('can get all ancestors and self')
->toContainOnlyInstancesOf(Group::class)
->toHaveCount(3);
-it('order all ancestors and self root first', function () use($make) {
+it('order all ancestors and self root first', function () use ($make) {
$g3 = $make($g2 = $make($g1 = $make()));
expect($g3->getAncestorsAndSelf())
@@ -81,17 +81,17 @@ it('order all ancestors and self root first', function () use($make) {
});
it('gets the path with no prefix or parents')
- ->expect(fn () => $make()->getPath())
+ ->expect(fn () => $make()->getFullPrefix())
->toBe('/');
it('gets the path without parents')
- ->expect(fn () => $make()->prefix('/t1/t2')->getPath())
+ ->expect(fn () => $make()->prefix('/t1/t2')->getFullPrefix())
->toBe('/t1/t2');
it('gets the path with parent')
->expect(fn () => $make(
$make()->prefix('hello/world')
- )->prefix('/t1/t2')->getPath())
+ )->prefix('/t1/t2')->getFullPrefix())
->toBe('/hello/world/t1/t2');
it('gets the path with two ancestors')
@@ -99,14 +99,13 @@ it('gets the path with two ancestors')
$make(
$make()->prefix('test')
)->prefix('hello/world')
- )->prefix('/t1/t2')->getPath())
+ )->prefix('/t1/t2')->getFullPrefix())
->toBe('/test/hello/world/t1/t2');
-
it('gets the path with three ancestors, one without prefix')
->expect(fn () => $make(
$make(
$make($make()->prefix('test'))
)->prefix('hello/world')
- )->prefix('/t1/t2')->getPath())
+ )->prefix('/t1/t2')->getFullPrefix())
->toBe('/test/hello/world/t1/t2');