1 | <?php declare(strict_types=1); |
||
10 | class Route |
||
11 | { |
||
12 | /** @var string */ |
||
13 | private $name; |
||
14 | |||
15 | /** @var array */ |
||
16 | private $attributes = []; |
||
17 | |||
18 | /** @var array */ |
||
19 | private $methods = []; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $path; |
||
23 | |||
24 | /** @var mixed */ |
||
25 | private $controller; |
||
26 | |||
27 | /** |
||
28 | * Route constructor. |
||
29 | * |
||
30 | * @param string $path |
||
31 | * @param string $name |
||
32 | * @param array $methods |
||
33 | */ |
||
34 | public function __construct(string $path, array $methods = ['GET'], string $name = '') |
||
35 | { |
||
36 | if (empty($name)) { |
||
37 | $name = self::generateNameFromPath($path); |
||
|
|||
38 | } |
||
39 | $this->name = $name; |
||
40 | $this->methods = $methods; |
||
41 | $this->path = $path; |
||
42 | } |
||
43 | |||
44 | public function getName(): string |
||
45 | { |
||
46 | return $this->name; |
||
47 | } |
||
48 | |||
49 | public function withController($controller): self |
||
50 | { |
||
51 | $instance = clone $this; |
||
52 | $instance->controller = $controller; |
||
53 | |||
54 | return $instance; |
||
55 | } |
||
56 | |||
57 | public function getController() |
||
58 | { |
||
59 | return $this->controller; |
||
60 | } |
||
61 | |||
62 | public function withMethods(array $methods): self |
||
63 | { |
||
64 | $instance = clone $this; |
||
65 | $instance->methods = $methods; |
||
66 | |||
67 | return $instance; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns route pattern. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getPath(): string |
||
79 | |||
80 | /** |
||
81 | * Returns request methods. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getMethods(): array |
||
86 | { |
||
87 | return $this->methods; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Factories new instance of the current route with |
||
92 | * attributes retrieved from client's request. |
||
93 | * |
||
94 | * @param array $attributes |
||
95 | * @return Route |
||
96 | */ |
||
97 | public function withAttributes(array $attributes): self |
||
98 | { |
||
99 | $instance = clone $this; |
||
100 | $instance->attributes = $attributes; |
||
101 | |||
102 | return $instance; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns attributes extracted from the uri. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getAttributes(): array |
||
111 | { |
||
112 | return $this->attributes; |
||
113 | } |
||
114 | |||
115 | public function getAttribute(string $name) |
||
119 | |||
120 | /** |
||
121 | * Factories new instance of the route |
||
122 | * that will be matched against get request. |
||
123 | * |
||
124 | * @param string $path |
||
125 | * @param string $name |
||
126 | * @return Route |
||
127 | */ |
||
128 | public static function get(string $path, string $name = ''): self |
||
129 | { |
||
130 | return new self($path, [Request::METHOD_GET], $name); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Factories new instance of the route |
||
135 | * that will be matched against post request. |
||
136 | * |
||
137 | * @param string $path |
||
138 | * @param string $name |
||
139 | * @return Route |
||
140 | */ |
||
141 | public static function post(string $path, string $name = ''): self |
||
142 | { |
||
143 | return new self($path, [Request::METHOD_POST], $name); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Factories new instance of the route |
||
148 | * that will be matched against put request. |
||
149 | * |
||
150 | * @param string $path |
||
151 | * @param string $name |
||
152 | * @return Route |
||
153 | */ |
||
154 | public static function put(string $path, string $name = ''): self |
||
155 | { |
||
156 | return new self($path, [Request::METHOD_PUT], $name); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Factories new instance of the route |
||
161 | * that will be matched against delete request. |
||
162 | * |
||
163 | * @param string $path |
||
164 | * @param string $name |
||
165 | * @return Route |
||
166 | */ |
||
167 | public static function delete(string $path, string $name = ''): self |
||
168 | { |
||
169 | return new self($path, [Request::METHOD_DELETE], $name); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Factories new instance of the route |
||
174 | * that will be matched against patch request. |
||
175 | * |
||
176 | * @param string $path |
||
177 | * @param string $name |
||
178 | * @return Route |
||
179 | */ |
||
180 | public static function patch(string $path, string $name = ''): self |
||
184 | |||
185 | /** |
||
186 | * Factories new instance of the route |
||
187 | * that will be matched against head request. |
||
188 | * |
||
189 | * @param string $path |
||
190 | * @param string $name |
||
191 | * @return Route |
||
192 | */ |
||
193 | public static function head(string $path, string $name = ''): self |
||
197 | |||
198 | /** |
||
199 | * Factories new instance of the route |
||
200 | * that will be matched against options request. |
||
201 | * |
||
202 | * @param string $path |
||
203 | * @param string $name |
||
204 | * @return Route |
||
205 | */ |
||
206 | public static function options(string $path, string $name = ''): self |
||
210 | |||
211 | /** |
||
212 | * Generates default name from given path expression, |
||
213 | * GET /some/{resource} becomes get_some_resource |
||
214 | * |
||
215 | * @param string $path |
||
216 | * @param array $methods |
||
217 | * @return string |
||
218 | */ |
||
219 | public static function generateNameFromPath(string $path, array $methods): string |
||
220 | { |
||
221 | $path = preg_replace('/<[^>]+>/', '', $path); |
||
222 | $uri = str_replace(['{', '}', '?', '.', '/'], ['', '', '', '_', '_'], trim($path, '/')); |
||
226 | } |
||
227 |
This check looks for function calls that miss required arguments.