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 | 5 | public function __construct(string $path, array $methods = ['GET'], string $name = '') |
|
43 | |||
44 | 1 | public function getName(): string |
|
48 | |||
49 | 1 | public function withController($controller): self |
|
56 | |||
57 | 1 | public function getController() |
|
61 | |||
62 | 1 | public function withMethods(array $methods): self |
|
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 | 1 | public function getMethods(): array |
|
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 | 1 | public function withAttributes(array $attributes): self |
|
104 | |||
105 | /** |
||
106 | * Returns attributes extracted from the uri. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | 1 | public function getAttributes(): array |
|
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 | 1 | public static function get(string $path, string $name = ''): self |
|
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 | 1 | public static function post(string $path, string $name = ''): self |
|
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 | 1 | public static function put(string $path, string $name = ''): self |
|
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 | 1 | public static function delete(string $path, string $name = ''): self |
|
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 | 5 | public static function generateNameFromPath(string $path, array $methods): string |
|
226 | } |
||
227 |