diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
| commit | 5d30e3e60f674781950ceaa7a411449a561cd214 (patch) | |
| tree | 91f043abc07a864036258e16b0c66027da74df01 | |
| parent | dc03419384af6def0b3adb3b976b1f5a3a46cca2 (diff) | |
build routes with with parameters
| -rw-r--r-- | src/PathSegment.php | 10 | ||||
| -rw-r--r-- | tests/Unit/PathSegmentTest.php | 34 |
2 files changed, 38 insertions, 6 deletions
diff --git a/src/PathSegment.php b/src/PathSegment.php index c2231da..d55102f 100644 --- a/src/PathSegment.php +++ b/src/PathSegment.php @@ -162,10 +162,16 @@ class PathSegment return $seg; } - public function getPath(): string + /** @param array<string, string> $parameters */ + public function getPath(array $parameters = []): string { $parts = array_map( - fn ($seg) => $seg->getContent(), + fn ($seg) => match ($seg->getType()) { + PathSegmentType::Parameter => ( + $parameters[$v = ($seg->getValue() ?? '')] ?? throw MissingParameterException::make($v) + ), + default => $seg->getContent(), + }, $this->getAncestorsAndSelf() ); diff --git a/tests/Unit/PathSegmentTest.php b/tests/Unit/PathSegmentTest.php index 0606b75..62c6ec9 100644 --- a/tests/Unit/PathSegmentTest.php +++ b/tests/Unit/PathSegmentTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); use Lightscale\Router\Enums\HttpMethod; use Lightscale\Router\Enums\PathSegmentType; +use Lightscale\Router\Exceptions\MissingParameterException; use Lightscale\Router\PathSegment; use Lightscale\Router\Route; @@ -146,10 +147,6 @@ it('gets the children count') ->expect(fn () => $makeWithChildren()->getChildrenCount()) ->toBe(2); -it('can build full path') - ->expect(fn () => $testChain()->getPath()) - ->toBe('/test1/test2/test3'); - it('creates new child and returns') ->expect(fn () => (new PathSegment('test1'))->child('test2')) ->toBeInstanceOf(PathSegment::class) @@ -191,6 +188,35 @@ it('can get a parameter segment child', function () { expect($seg->findChild('test4'))->toBe($c2); }); +it('can build full path') + ->expect(fn () => $testChain()->getPath()) + ->toBe('/test1/test2/test3'); + +it('can build path with parameters', function() { + $seg = new PathSegment(type: PathSegmentType::Root); + $child = $seg->child('test1') + ->child('test2', PathSegmentType::Parameter) + ->child('test3', PathSegmentType::Parameter) + ->child('test4'); + + expect($child->getPath([ + 'test2' => 'a', + 'test3' => 'b', + ]))->toBe('/test1/a/b/test4'); +}); + +it('throws when building path with missing parameters', function() { + $seg = new PathSegment(type: PathSegmentType::Root); + $child = $seg->child('test1') + ->child('test2', PathSegmentType::Parameter) + ->child('test3', PathSegmentType::Parameter) + ->child('test4'); + + expect($child->getPath([ + 'test2' => 'a', + ]))->toBe('/test1/a/b/test4'); +})->throws(MissingParameterException::class); + it('sets route segment when added', function () { $seg = new PathSegment('test1'); $seg->addRoute($r = new Route( |
