1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Framework. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Describe valid routes. |
16
|
|
|
*/ |
17
|
|
|
class Route implements RouteInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string Route name. |
21
|
|
|
*/ |
22
|
|
|
public string $name = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string Route method. |
26
|
|
|
*/ |
27
|
|
|
public string $method = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string Route url. |
31
|
|
|
*/ |
32
|
|
|
public string $url = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string Route model. |
36
|
|
|
*/ |
37
|
|
|
public string $model = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string Route view. |
41
|
|
|
*/ |
42
|
|
|
public string $view = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Route controller. |
46
|
|
|
*/ |
47
|
|
|
public string $controller = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string Route action. |
51
|
|
|
*/ |
52
|
|
|
public string $action = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool Route default. |
56
|
|
|
*/ |
57
|
|
|
public bool $default = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array<mixed> Route parameters. |
61
|
|
|
*/ |
62
|
|
|
public array $param = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var callable Route callback. |
66
|
|
|
*/ |
67
|
|
|
public $callback; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string Ip address from which the route is callable, ip address |
71
|
|
|
* format is intentionally not specified. |
72
|
|
|
*/ |
73
|
|
|
public string $allowed = '*'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Constructor. |
77
|
|
|
* |
78
|
|
|
* @param array<mixed> $route |
79
|
|
|
*/ |
80
|
28 |
|
public function __construct(array $route = []) |
81
|
|
|
{ |
82
|
|
|
[ |
83
|
28 |
|
'name' => $this->name, |
84
|
28 |
|
'method' => $this->method, |
85
|
28 |
|
'url' => $this->url, |
86
|
28 |
|
'model' => $this->model, |
87
|
28 |
|
'view' => $this->view, |
88
|
28 |
|
'controller' => $this->controller, |
89
|
28 |
|
'action' => $this->action, |
90
|
28 |
|
'default' => $this->default, |
91
|
28 |
|
'param' => $this->param, |
92
|
28 |
|
'callback' => $this->callback |
93
|
28 |
|
] = \array_replace_recursive([ |
94
|
28 |
|
'name' => $this->name, |
95
|
28 |
|
'method' => $this->method, |
96
|
28 |
|
'url' => $this->url, |
97
|
28 |
|
'model' => $this->model, |
98
|
28 |
|
'view' => $this->view, |
99
|
28 |
|
'controller' => $this->controller, |
100
|
28 |
|
'action' => $this->action, |
101
|
28 |
|
'default' => $this->default, |
102
|
28 |
|
'param' => $this->param, |
103
|
28 |
|
'callback' => $this->callback, |
104
|
28 |
|
], $route); |
105
|
28 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return route name. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
6 |
|
public function getName(): string |
113
|
|
|
{ |
114
|
6 |
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return route method. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
public function getMethod(): string |
123
|
|
|
{ |
124
|
1 |
|
return $this->method; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return route url. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
1 |
|
public function getUrl(): string |
133
|
|
|
{ |
134
|
1 |
|
return $this->url; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return model name. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
1 |
|
public function getModel(): string |
143
|
|
|
{ |
144
|
1 |
|
return $this->model; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return view name. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
1 |
|
public function getView(): string |
153
|
|
|
{ |
154
|
1 |
|
return $this->view; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return controller. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
1 |
|
public function getController(): string |
163
|
|
|
{ |
164
|
1 |
|
return $this->controller; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return action name. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
2 |
|
public function getAction(): string |
173
|
|
|
{ |
174
|
2 |
|
return $this->action; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return parameters. |
179
|
|
|
* |
180
|
|
|
* @return array<mixed> |
181
|
|
|
*/ |
182
|
2 |
|
public function getParam(): array |
183
|
|
|
{ |
184
|
2 |
|
return $this->param; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return if route is set as default. |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
1 |
|
public function isDefault(): bool |
193
|
|
|
{ |
194
|
1 |
|
return $this->default; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return route callback. |
199
|
|
|
* |
200
|
|
|
* @return callable |
201
|
|
|
*/ |
202
|
11 |
|
public function getCallback(): callable |
203
|
|
|
{ |
204
|
11 |
|
if (\is_callable($this->callback)) { |
205
|
10 |
|
return $this->callback; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
return function () { |
209
|
1 |
|
}; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return Ip addresses from which the route is callable. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getAllowed(): string |
218
|
|
|
{ |
219
|
|
|
return $this->allowed; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Return route array. |
224
|
|
|
* |
225
|
|
|
* @return array<mixed> |
226
|
|
|
*/ |
227
|
1 |
|
public function toArray(): array |
228
|
|
|
{ |
229
|
|
|
return [ |
230
|
1 |
|
'name' => $this->name, |
231
|
1 |
|
'method' => $this->method, |
232
|
1 |
|
'url' => $this->url, |
233
|
1 |
|
'model' => $this->model, |
234
|
1 |
|
'view' => $this->view, |
235
|
1 |
|
'controller' => $this->controller, |
236
|
1 |
|
'action' => $this->action, |
237
|
1 |
|
'default' => $this->default, |
238
|
1 |
|
'param' => $this->param, |
239
|
1 |
|
'callback' => $this->callback |
240
|
|
|
]; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|