summaryrefslogtreecommitdiff
path: root/src/PathSegment.php
blob: 01f3fc7ed9450b71eabd30cb955448031cca3bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

declare(strict_types=1);

namespace Lightscale\Router;

use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Enums\PathSegmentType;
use Lightscale\Router\Exceptions\MissingParameterException;

class PathSegment
{
    use Concerns\HasAncestors;

    /** @var array<string, self> */
    protected array $children;

    /** @var array<value-of<HttpMethod>, Route> */
    protected array $routes = [];

    protected ?self $parent = null;

    final public function __construct(
        protected ?string $value = null,
        protected PathSegmentType $type = PathSegmentType::Raw,
    ) {
        if (PathSegmentType::Root === $type) {
            $this->value = null;
        }
    }

    public function getContent(): string
    {
        return match ($this->type) {
            PathSegmentType::Root => '',
            default => $this->value ?? '',
        };
    }

    public function getValue(): ?string
    {
        return $this->value;
    }

    private static function createKey(
        ?string $value,
        PathSegmentType $type,
    ): string {
        return match ($type) {
            PathSegmentType::Raw => strtolower($value ?? ''),
            PathSegmentType::Root => '<root>',
            PathSegmentType::Parameter => '<parameter>',
        };
    }

    public function getKey(): string
    {
        return self::createKey($this->value, $this->type);
    }

    public function getType(): PathSegmentType
    {
        return $this->type;
    }

    public function setParent(?self $segment): void
    {
        $this->parent = $segment;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function addChild(self $segment): void
    {
        $segment->setParent($this);
        $this->children[$segment->getKey()] = $segment;
    }

    /** @return array<string, self> */
    public function getChildren(): array
    {
        return $this->children;
    }

    public function getChildrenCount(): int
    {
        return count($this->children);
    }

    public function findChild(
        ?string $value = null,
    ): ?PathSegment {
        $key = self::createKey($value, PathSegmentType::Raw);
        $seg = $this->children[$key] ?? null;

        if ($seg instanceof self) {
            return $seg;
        }

        $key = self::createKey($value, PathSegmentType::Parameter);
        $seg = $this->children[$key] ?? null;

        return $seg;
    }

    public function child(
        ?string $value = null,
        PathSegmentType $type = PathSegmentType::Raw,
    ): self {
        $key = self::createKey($value, $type);
        $seg = $this->children[$key] ?? null;

        if (null === $seg) {
            $seg = new PathSegment($value, $type);
        }

        $this->addChild($seg);

        return $seg;
    }

    /** @param array<string, string> $parameters */
    public function getPath(array $parameters = []): string
    {
        $parts = array_map(
            fn ($seg) => match ($seg->getType()) {
                PathSegmentType::Parameter => (
                    $parameters[$v = ($seg->getValue() ?? '')] ?? throw MissingParameterException::make($v)
                ),
                default => $seg->getContent(),
            },
            $this->getAncestorsAndSelf()
        );

        return implode('/', $parts);
    }

    public function addRoute(Route $route): void
    {
        $route->setSegment($this);
        $method = $route->getMethod()->value;
        $this->routes[$method] = $route;
    }

    public function getRoute(HttpMethod $method): ?Route
    {
        return $this->routes[$method->value] ?? null;
    }

    /** @return array<value-of<HttpMethod>, Route> */
    public function getRoutes(): array
    {
        return $this->routes;
    }
}