1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Route |
21
|
|
|
*/ |
22
|
|
|
class Route implements RouteInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The route ID |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The route path |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $path; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The route methods |
41
|
|
|
* |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
protected $methods = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The route patterns |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $patterns = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The route attributes |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $attributes = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The route middleware stack |
62
|
|
|
* |
63
|
|
|
* @var MiddlewareInterface[] |
64
|
|
|
*/ |
65
|
|
|
protected $middlewareStack = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Constructor of the class |
69
|
|
|
* |
70
|
|
|
* @param string $id |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string[] $methods |
73
|
|
|
*/ |
74
|
|
|
public function __construct(string $id, string $path, array $methods) |
75
|
|
|
{ |
76
|
|
|
$this->setId($id); |
77
|
|
|
|
78
|
|
|
$this->setPath($path); |
79
|
|
|
|
80
|
|
|
foreach ($methods as $method) |
81
|
|
|
{ |
82
|
|
|
$this->addMethod($method); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function setId(string $id) : RouteInterface |
90
|
|
|
{ |
91
|
|
|
$this->id = $id; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public function setPath(string $path) : RouteInterface |
100
|
|
|
{ |
101
|
|
|
$this->path = $path; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function addPrefix(string $prefix) : RouteInterface |
110
|
|
|
{ |
111
|
|
|
$this->path = $prefix . $this->path; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function addSuffix(string $suffix) : RouteInterface |
120
|
|
|
{ |
121
|
|
|
$this->path .= $suffix; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
|
|
public function addMethod(string $method) : RouteInterface |
130
|
|
|
{ |
131
|
|
|
$this->methods[] = \strtoupper($method); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function addPattern(string $name, string $value) : RouteInterface |
140
|
|
|
{ |
141
|
|
|
$this->patterns[$name] = $value; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
|
|
public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface |
150
|
|
|
{ |
151
|
|
|
$this->middlewareStack[] = $middleware; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
|
|
public function getId() : string |
160
|
|
|
{ |
161
|
|
|
return $this->id; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function getPath() : string |
168
|
|
|
{ |
169
|
|
|
return $this->path; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function getMethods() : array |
176
|
|
|
{ |
177
|
|
|
return $this->methods; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
*/ |
183
|
|
|
public function getPatterns() : array |
184
|
|
|
{ |
185
|
|
|
return $this->patterns; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritDoc} |
190
|
|
|
*/ |
191
|
|
|
public function getAttributes() : array |
192
|
|
|
{ |
193
|
|
|
return $this->attributes; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritDoc} |
198
|
|
|
*/ |
199
|
|
|
public function getMiddlewareStack() : array |
200
|
|
|
{ |
201
|
|
|
return $this->middlewareStack; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritDoc} |
206
|
|
|
*/ |
207
|
|
|
public function withAttributes(array $attributes) : RouteInterface |
208
|
|
|
{ |
209
|
|
|
$clone = clone $this; |
210
|
|
|
|
211
|
|
|
$clone->attributes = \array_merge($clone->attributes, $attributes); |
212
|
|
|
|
213
|
|
|
return $clone; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|