summaryrefslogtreecommitdiff
path: root/src/Route.php
blob: aeb3f664ee7e8e30b853e500f6ea94b2731a5fba (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
<?php

declare(strict_types=1);

namespace Lightscale\Router;

use Closure;
use Lightscale\Router\Enums\HttpMethod;
use Psr\Http\Message\RequestInterface;

class Route
{
    protected Closure $handler;

    protected PathSegment $segment;

    public function __construct(
        protected HttpMethod $method,
        callable $handler,
        ?PathSegment $segment = null,
    ) {
        $this->handler = Closure::fromCallable($handler);

        if (null !== $segment) {
            $this->segment = $segment;
        }
    }

    public function __invoke(RequestInterface $request): mixed
    {
        return ($this->handler)($request);
    }

    public function getMethod(): HttpMethod
    {
        return $this->method;
    }

    public function setSegment(PathSegment $segment): void
    {
        $this->segment = $segment;
    }

    public function getSegment(): PathSegment
    {
        return $this->segment;
    }

    public function getPath(): string
    {
        return $this->segment->getPath();
    }
}