summaryrefslogtreecommitdiff
path: root/src/Enums/PathSegmentType.php
blob: 7cb1a516c83b4435d41995dbf9a40404f5f495a4 (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
<?php

declare(strict_types=1);

namespace Lightscale\Router\Enums;

use Lightscale\Router\PathSegmentTypeMatch;

enum PathSegmentType
{
    case Raw;
    case Root;
    case Parameter;

    public function routeMatchRegex(): ?string
    {
        return match ($this) {
            self::Parameter => '/{([a-zA-Z0-9-_]+)}/',
            default => null,
        };
    }

    public static function matchRouteString(string $value): PathSegmentTypeMatch
    {
        $type = PathSegmentType::Parameter;
        $regex = $type->routeMatchRegex();
        if (preg_match($regex, $value, $matches) === 1) {
            $value = $matches[1];
        }
        else {
            $type = PathSegmentType::Raw;
        }
        return new PathSegmentTypeMatch( $type, $value);
    }
}