summaryrefslogtreecommitdiff
path: root/src/PathSegment.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/PathSegment.php')
-rw-r--r--src/PathSegment.php63
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
+ {
+ }
}