1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\RouteViewer\Entities; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\{Arrayable, Jsonable}; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Route |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Route implements Arrayable, Jsonable, JsonSerializable |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
public $methods = []; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public $uri; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
public $params = []; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $action; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
public $name; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
public $middleware; |
39
|
|
|
|
40
|
|
|
/** @var string|null */ |
41
|
|
|
public $domain; |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Constructor |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Route constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $methods |
52
|
|
|
* @param string $uri |
53
|
|
|
* @param string|null $name |
54
|
|
|
* @param string|null $action |
55
|
|
|
* @param array $middleware |
56
|
|
|
* @param string|null $domain |
57
|
|
|
*/ |
58
|
28 |
|
public function __construct(array $methods, $uri, $action, $name, array $middleware, $domain = null) |
59
|
|
|
{ |
60
|
28 |
|
$this->methods = $methods; |
61
|
28 |
|
$this->setUri($uri); |
62
|
28 |
|
$this->action = $action; |
63
|
28 |
|
$this->name = $name; |
64
|
28 |
|
$this->middleware = $middleware; |
65
|
28 |
|
$this->domain = $domain; |
66
|
28 |
|
} |
67
|
|
|
|
68
|
|
|
/* ----------------------------------------------------------------- |
69
|
|
|
| Getters & Setters |
70
|
|
|
| ----------------------------------------------------------------- |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the route URI. |
75
|
|
|
* |
76
|
|
|
* @param string $uri |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
28 |
|
private function setUri(string $uri) |
81
|
|
|
{ |
82
|
28 |
|
$this->uri = $uri; |
83
|
|
|
|
84
|
28 |
|
preg_match_all('/({[^}]+})/', $this->uri, $matches); |
85
|
28 |
|
$this->params = $matches[0]; |
86
|
|
|
|
87
|
28 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the action namespace. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
8 |
|
public function getActionNamespace(): string |
96
|
|
|
{ |
97
|
8 |
|
return $this->isClosure() ? '' : explode('@', $this->action)[0]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the action method. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
8 |
|
public function getActionMethod(): string |
106
|
|
|
{ |
107
|
8 |
|
return $this->isClosure() ? '' : explode('@', $this->action)[1]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* ----------------------------------------------------------------- |
111
|
|
|
| Check Methods |
112
|
|
|
| ----------------------------------------------------------------- |
113
|
|
|
*/ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check if the route has name. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
8 |
|
public function hasName(): bool |
121
|
|
|
{ |
122
|
8 |
|
return ! is_null($this->name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if the route has domain. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
4 |
|
public function hasDomain(): bool |
131
|
|
|
{ |
132
|
4 |
|
return ! is_null($this->domain); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if the route has middleware. |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
4 |
|
public function hasMiddleware(): bool |
141
|
|
|
{ |
142
|
4 |
|
return ! empty($this->middleware); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if the action is a closure function. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
12 |
|
public function isClosure(): bool |
151
|
|
|
{ |
152
|
12 |
|
return $this->action === 'Closure'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* ----------------------------------------------------------------- |
156
|
|
|
| Other Methods |
157
|
|
|
| ----------------------------------------------------------------- |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the instance as an array. |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
8 |
|
public function toArray(): array |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
8 |
|
'methods' => $this->methods, |
169
|
8 |
|
'uri' => $this->uri, |
170
|
8 |
|
'params' => $this->params, |
171
|
8 |
|
'action' => $this->action, |
172
|
8 |
|
'name' => $this->name, |
173
|
8 |
|
'middleware' => $this->middleware, |
174
|
8 |
|
'domain' => $this->domain, |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Convert the object into something JSON serializable. |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
4 |
|
public function jsonSerialize(): array |
184
|
|
|
{ |
185
|
4 |
|
return $this->toArray(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Convert the object to its JSON representation. |
190
|
|
|
* |
191
|
|
|
* @param int $options |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
4 |
|
public function toJson($options = 0): string |
196
|
|
|
{ |
197
|
4 |
|
return json_encode($this->jsonSerialize(), $options); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|