summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/PathSegment.php16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/PathSegment.php b/src/PathSegment.php
index 9337fa1..c2231da 100644
--- a/src/PathSegment.php
+++ b/src/PathSegment.php
@@ -6,6 +6,7 @@ namespace Lightscale\Router;
use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Enums\PathSegmentType;
+use Lightscale\Router\Exceptions\MissingParameterException;
class PathSegment
{
@@ -19,7 +20,7 @@ class PathSegment
protected ?self $parent = null;
- public function __construct(
+ final public function __construct(
protected ?string $value = null,
protected PathSegmentType $type = PathSegmentType::Raw,
) {
@@ -28,6 +29,19 @@ 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) {