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\Annotation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Import functions |
22
|
|
|
*/ |
23
|
|
|
use function class_exists; |
24
|
|
|
use function is_array; |
25
|
|
|
use function is_int; |
26
|
|
|
use function is_string; |
27
|
|
|
use function is_subclass_of; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @Annotation |
31
|
|
|
* @Target({"CLASS"}) |
32
|
|
|
*/ |
33
|
|
|
final class Route |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var object |
38
|
|
|
*/ |
39
|
|
|
public $source; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $path; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
public $methods; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
public $middlewares; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
public $attributes; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
public $priority; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $params |
73
|
|
|
*/ |
74
|
17 |
|
public function __construct(array $params) |
75
|
|
|
{ |
76
|
17 |
|
$this->assertValidName($params); |
77
|
14 |
|
$this->assertValidPath($params); |
78
|
11 |
|
$this->assertValidMethods($params); |
79
|
7 |
|
$this->assertValidMiddlewares($params); |
80
|
3 |
|
$this->assertValidAttributes($params); |
81
|
2 |
|
$this->assertValidPriority($params); |
82
|
|
|
|
83
|
1 |
|
$this->name = $params['name']; |
84
|
1 |
|
$this->path = $params['path']; |
85
|
1 |
|
$this->methods = $params['methods']; |
86
|
1 |
|
$this->middlewares = $params['middlewares'] ?? []; |
87
|
1 |
|
$this->attributes = $params['attributes'] ?? []; |
88
|
1 |
|
$this->priority = $params['priority'] ?? 0; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $params |
93
|
|
|
* @return void |
94
|
|
|
* @throws InvalidArgumentException |
95
|
|
|
*/ |
96
|
17 |
|
private function assertValidName(array $params) : void |
97
|
|
|
{ |
98
|
17 |
|
if (empty($params['name']) || !is_string($params['name'])) { |
99
|
3 |
|
throw new InvalidArgumentException('@Route.name must be not an empty string.'); |
100
|
|
|
} |
101
|
14 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $params |
105
|
|
|
* @return void |
106
|
|
|
* @throws InvalidArgumentException |
107
|
|
|
*/ |
108
|
14 |
|
private function assertValidPath(array $params) : void |
109
|
|
|
{ |
110
|
14 |
|
if (empty($params['path']) || !is_string($params['path'])) { |
111
|
3 |
|
throw new InvalidArgumentException('@Route.path must be not an empty string.'); |
112
|
|
|
} |
113
|
11 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $params |
117
|
|
|
* @return void |
118
|
|
|
* @throws InvalidArgumentException |
119
|
|
|
*/ |
120
|
11 |
|
private function assertValidMethods(array $params) : void |
121
|
|
|
{ |
122
|
11 |
|
if (empty($params['methods']) || !is_array($params['methods'])) { |
123
|
3 |
|
throw new InvalidArgumentException('@Route.methods must be not an empty array.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
foreach ($params['methods'] as $method) { |
127
|
8 |
|
if (!is_string($method)) { |
128
|
8 |
|
throw new InvalidArgumentException('@Route.methods must contain only strings.'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
7 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $params |
135
|
|
|
* @return void |
136
|
|
|
* @throws InvalidArgumentException |
137
|
|
|
*/ |
138
|
7 |
|
private function assertValidMiddlewares(array $params) : void |
139
|
|
|
{ |
140
|
7 |
|
if (!isset($params['middlewares'])) { |
141
|
2 |
|
return; |
142
|
5 |
|
} elseif (!is_array($params['middlewares'])) { |
143
|
1 |
|
throw new InvalidArgumentException('@Route.middlewares must be an array.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
4 |
|
foreach ($params['middlewares'] as $middleware) { |
147
|
4 |
|
if (!is_string($middleware)) { |
148
|
1 |
|
throw new InvalidArgumentException('@Route.middlewares must contain only strings.'); |
149
|
4 |
|
} elseif (!class_exists($middleware)) { |
150
|
1 |
|
throw new InvalidArgumentException('@Route.middlewares contains nonexistent class.'); |
151
|
3 |
|
} elseif (!is_subclass_of($middleware, MiddlewareInterface::class)) { |
152
|
3 |
|
throw new InvalidArgumentException('@Route.middlewares contains non middleware class.'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $params |
159
|
|
|
* @return void |
160
|
|
|
* @throws InvalidArgumentException |
161
|
|
|
*/ |
162
|
3 |
|
private function assertValidAttributes(array $params) : void |
163
|
|
|
{ |
164
|
3 |
|
if (!isset($params['attributes'])) { |
165
|
1 |
|
return; |
166
|
2 |
|
} elseif (!is_array($params['attributes'])) { |
167
|
1 |
|
throw new InvalidArgumentException('@Route.attributes must be an array.'); |
168
|
|
|
} |
169
|
1 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $params |
173
|
|
|
* @return void |
174
|
|
|
* @throws InvalidArgumentException |
175
|
|
|
*/ |
176
|
2 |
|
private function assertValidPriority(array $params) : void |
177
|
|
|
{ |
178
|
2 |
|
if (isset($params['priority']) && !is_int($params['priority'])) { |
179
|
1 |
|
throw new InvalidArgumentException('@Route.priority must be an integer.'); |
180
|
|
|
} |
181
|
1 |
|
} |
182
|
|
|
} |
183
|
|
|
|