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 | 48431dd32b3f73855f9463af41dba997a2962f9f (patch) | |
| tree | 654b8f6d5466aeef5fb36495c46e389c27d32eb9 /src/PathSegment.php | |
| parent | a2eaccfa4d970bad833110337650904ae125642a (diff) | |
Added more methods to the PathSegment class and tests
Diffstat (limited to 'src/PathSegment.php')
| -rw-r--r-- | src/PathSegment.php | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/src/PathSegment.php b/src/PathSegment.php index e57667b..7f24660 100644 --- a/src/PathSegment.php +++ b/src/PathSegment.php @@ -4,19 +4,26 @@ declare(strict_types=1); namespace Lightscale\Router; +use Lightscale\Router\Enums\HttpMethod; use Lightscale\Router\Enums\SpecialSegment; class PathSegment { + private const MAX_ANCESTORY_DEPTH = 100; + protected string $content; protected ?SpecialSegment $specialSegment = null; - /** @var array<string, static> */ - protected array $segments; + /** @var array<string, self> */ + protected array $children; + + /** @var array<value-of<HttpMethod>, Route[]> */ + protected array $routes = []; public function __construct( SpecialSegment|string $content, + protected ?self $parent = null, ) { if ($content instanceof SpecialSegment) { $this->content = $content->value; @@ -25,4 +32,56 @@ class PathSegment $this->content = $content; } } + + public function getContent(): string + { + return $this->content; + } + + public function getParent(): ?self + { + return $this->parent; + } + + /** @return self[] */ + public function getAncestors(): array + { + $results = []; + + $count = 0; + $instance = $this->parent; + while ( + null !== $instance + && $count++ < self::MAX_ANCESTORY_DEPTH + ) { + $results[] = $instance; + $instance = $instance->parent; + } + + return array_reverse($results); + } + + /** @return self[] */ + public function getAncestorsAndSelf(): array + { + $results = $this->getAncestors(); + $results[] = $this; + + return $results; + } + + public function addChild(self $segment): void + { + $this->children[$segment->getContent()] = $segment; + } + + /** @return array<string, self> */ + public function getChildren(): array + { + return $this->children; + } + + public function addRoute(): void + { + } } |
