diff options
| -rw-r--r-- | src/PathSegment.php | 63 | ||||
| -rw-r--r-- | tests/Unit/PathSegmentTest.php | 68 | ||||
| -rw-r--r-- | tests/Unit/RouteTest.php | 2 |
3 files changed, 130 insertions, 3 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 + { + } } diff --git a/tests/Unit/PathSegmentTest.php b/tests/Unit/PathSegmentTest.php new file mode 100644 index 0000000..1d19e0e --- /dev/null +++ b/tests/Unit/PathSegmentTest.php @@ -0,0 +1,68 @@ +<?php + +declare(strict_types=1); + +use Lightscale\Router\PathSegment; + +it('initializes') + ->expect(fn () => new PathSegment('test')) + ->toBeInstanceOf(PathSegment::class); + +it('can get the content') + ->expect(fn () => (new PathSegment('test'))->getContent()) + ->toBe('test'); + +it('can get the parent') + ->expect(fn () => (new PathSegment( + 'test1', + new PathSegment('test2') + ))->getParent()) + ->toBeInstanceOf(PathSegment::class) + ->getContent()->toBe('test2'); + +it('has null parent by default') + ->expect(fn () => (new PathSegment('test'))->getParent()) + ->toBeNull(); + +$testChain = fn () => new PathSegment( + 'test3', + new PathSegment( + 'test2', + new PathSegment( + 'test1' + ) + ) +); + +it('can get all ancestors') + ->expect(fn () => $testChain()->getAncestors()) + ->toBeArray() + ->toContainOnlyInstancesOf(PathSegment::class) + ->toHaveCount(2); + +it('order all ancestors root first') + ->expect(fn () => $testChain()->getAncestors()) + ->{0}->getContent()->toBe('test1') + ->{1}->getContent()->toBe('test2'); + +it('can get all ancestors and self') + ->expect(fn () => $testChain()->getAncestorsAndSelf()) + ->toBeArray() + ->toContainOnlyInstancesOf(PathSegment::class) + ->toHaveCount(3); + +it('order all ancestors and self root first') + ->expect(fn () => $testChain()->getAncestorsAndSelf()) + ->{0}->getContent()->toBe('test1') + ->{1}->getContent()->toBe('test2') + ->{2}->getContent()->toBe('test3'); + +it('can have children')->todo(); + +it('can build full path')->todo(); + +it('can have routes')->todo(); + +it('can get routes by method')->todo(); + +it('can get routes for all methods')->todo(); diff --git a/tests/Unit/RouteTest.php b/tests/Unit/RouteTest.php index 5e3dc3b..0414649 100644 --- a/tests/Unit/RouteTest.php +++ b/tests/Unit/RouteTest.php @@ -13,4 +13,4 @@ it('initializes') HttpMethod::Get, fn () => null )) - ->toBeInstance(Route::class); + ->toBeInstanceOf(Route::class); |
