Issues (6)

src/Routing/Route.php (1 issue)

1
<?php
2
3
namespace MiladRahimi\PhpRouter\Routing;
4
5
use Closure;
6
7
/**
8
 * It is a single defined route
9
 */
10
class Route
11
{
12
    /**
13
     * The route name
14
     */
15
    private ?string $name;
16
17
    /**
18
     * The route path
19
     */
20
    private string $path;
21
22
    /**
23
     * The route http method
24
     */
25
    private ?string $method;
26
27
    /**
28
     * The route controller
29
     *
30
     * @var Closure|string|array
31
     */
32
    private $controller;
33
34
    /**
35
     * The route middleware
36
     */
37
    private array $middleware;
38
39
    /**
40
     * The route domain
41
     */
42
    private ?string $domain;
43
44
    /**
45
     * The route uri from user request (post-property)
46
     */
47
    private ?string $uri = null;
48
49
    /**
50
     * The route parameters from user request (post-property)
51
     *
52
     * @var string[]
53
     */
54
    private array $parameters = [];
55
56
    /**
57
     * Constructor
58
     *
59
     * @param string|null $name
60
     * @param string $path
61
     * @param string|null $method
62
     * @param Closure|string|array $controller
63
     * @param array $middleware
64
     * @param string|null $domain
65
     */
66
    public function __construct(
67
        string $method,
68
        string $path,
69
        $controller,
70
        ?string $name,
71
        array $middleware,
72
        ?string $domain
73
    )
74
    {
75
        $this->method = $method;
76
        $this->path = $path;
77
        $this->controller = $controller;
78
        $this->name = $name;
79
        $this->middleware = $middleware;
80
        $this->domain = $domain;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function toArray(): array
87
    {
88
        return [
89
            'method' => $this->getMethod(),
90
            'path' => $this->getPath(),
91
            'controller' => $this->getController(),
92
            'name' => $this->getName(),
93
            'middleware' => $this->getMiddleware(),
94
            'domain' => $this->getDomain(),
95
            'uri' => $this->getUri(),
96
            'parameters' => $this->getParameters(),
97
        ];
98
    }
99
100
    public function toJson(): string
101
    {
102
        return json_encode($this->toArray());
103
    }
104
105
    public function __toString(): string
106
    {
107
        return $this->toJson();
108
    }
109
110
    public function getName(): ?string
111
    {
112
        return $this->name;
113
    }
114
115
    public function getPath(): string
116
    {
117
        return $this->path;
118
    }
119
120
    public function getMethod(): string
121
    {
122
        return $this->method;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->method could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
123
    }
124
125
    public function getController()
126
    {
127
        return $this->controller;
128
    }
129
130
    public function getMiddleware(): array
131
    {
132
        return $this->middleware;
133
    }
134
135
    public function getDomain(): ?string
136
    {
137
        return $this->domain;
138
    }
139
140
    public function getParameters(): array
141
    {
142
        return $this->parameters;
143
    }
144
145
    public function setParameters(array $parameters): void
146
    {
147
        $this->parameters = $parameters;
148
    }
149
150
    public function getUri(): ?string
151
    {
152
        return $this->uri;
153
    }
154
155
    public function setUri(?string $uri): void
156
    {
157
        $this->uri = $uri;
158
    }
159
}
160