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 | ac836311005458a10dded324420be92f44976332 (patch) | |
| tree | f456556ceac141aaaeeb0093c4438a44b99e7148 /src | |
| parent | c762531c8679dbb800346174913bfa21917ac0b5 (diff) | |
generating routes with parameters and tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/Enums/PathSegmentType.php | 15 | ||||
| -rw-r--r-- | src/PathSegment.php | 13 | ||||
| -rw-r--r-- | src/PathSegmentTypeMatch.php | 13 | ||||
| -rw-r--r-- | src/Router.php | 3 |
4 files changed, 30 insertions, 14 deletions
diff --git a/src/Enums/PathSegmentType.php b/src/Enums/PathSegmentType.php index 43113e6..7cb1a51 100644 --- a/src/Enums/PathSegmentType.php +++ b/src/Enums/PathSegmentType.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Lightscale\Router\Enums; +use Lightscale\Router\PathSegmentTypeMatch; + enum PathSegmentType { case Raw; @@ -17,4 +19,17 @@ enum PathSegmentType default => null, }; } + + public static function matchRouteString(string $value): PathSegmentTypeMatch + { + $type = PathSegmentType::Parameter; + $regex = $type->routeMatchRegex(); + if (preg_match($regex, $value, $matches) === 1) { + $value = $matches[1]; + } + else { + $type = PathSegmentType::Raw; + } + return new PathSegmentTypeMatch( $type, $value); + } } diff --git a/src/PathSegment.php b/src/PathSegment.php index d55102f..2fc4cbb 100644 --- a/src/PathSegment.php +++ b/src/PathSegment.php @@ -29,19 +29,6 @@ class PathSegment } } - public static function makeFromRouteString(string $value): static - { - $type = PathSegmentType::Parameter; - $regex = $type->routeMatchRegex(); - if (preg_match($regex, $value, $matches) === 1) { - $value = $matches[1]; - } - else { - $type = PathSegmentType::Raw; - } - return new static($value, $type); - } - public function getContent(): string { return match ($this->type) { diff --git a/src/PathSegmentTypeMatch.php b/src/PathSegmentTypeMatch.php new file mode 100644 index 0000000..4e32bdb --- /dev/null +++ b/src/PathSegmentTypeMatch.php @@ -0,0 +1,13 @@ +<?php + +namespace Lightscale\Router; + +use Lightscale\Router\Enums\PathSegmentType; + +readonly class PathSegmentTypeMatch +{ + final public function __construct( + public PathSegmentType $type, + public string $value, + ) {} +} diff --git a/src/Router.php b/src/Router.php index 4463150..1c5e087 100644 --- a/src/Router.php +++ b/src/Router.php @@ -126,7 +126,8 @@ class Router $seg = $this->root(); while (($v = array_shift($pathSplit)) !== null) { - $seg = $seg->child($v); + $typeMatch = PathSegmentType::matchRouteString($v); + $seg = $seg->child($typeMatch->value, $typeMatch->type); } $route = new Route($method, $handler); |
