This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RouteBuilder.php
298 lines (267 loc) · 7.01 KB
/
RouteBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* This file is part of the Pathfinder package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace bitExpert\Pathfinder;
/**
* This class wraps route creation in order to offer a convenient readable way for defining a route
* without allowing the route to be in an inconsistent state.
*/
class RouteBuilder
{
/**
* @var string
*/
protected static $defaultRouteClass = Route::class;
/**
* @var string
*/
protected $routeClass;
/**
* @var string[]
*/
protected $methods;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $target;
/**
* @var callable[]
*/
protected $matchers;
/**
* @var string
*/
protected $name;
/**
* RouteBuilder constructor.
*
* @param string $routeClass The route class to build the route from
* @throws \InvalidArgumentException
*/
protected function __construct($routeClass)
{
if (!is_a($routeClass, Route::class, true)) {
throw new \InvalidArgumentException(sprintf(
'The configured route class "%s" is not of or does not inherit class "%s"',
$routeClass,
Route::class
));
}
$this->routeClass = $routeClass;
$this->path = null;
$this->target = null;
$this->name = null;
$this->methods = [];
$this->matchers = [];
}
/**
* Sets the route class to build the route from globally.
*
* @param string $defaultRouteClass
*/
public static function setDefaultRouteClass($defaultRouteClass = Route::class)
{
self::$defaultRouteClass = $defaultRouteClass;
}
/**
* Creates a new {@link bitExpert\Pathfinder\RouteBuilder} instance using the given
* $routeClass to create a new route from.
*
* @param string|null $routeClass
* @return RouteBuilder
*/
public static function route(string $routeClass = null) : RouteBuilder
{
$routeClass = $routeClass ? $routeClass : self::$defaultRouteClass;
return new static($routeClass);
}
/**
* Builds a route using the provided configuration.
*
* @return Route
*/
public function build() : Route
{
return new $this->routeClass($this->methods, $this->path, $this->target, $this->matchers, $this->name);
}
/**
* Creates a new HEAD accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function head(string $path) : RouteBuilder
{
return $this->from($path)->accepting('HEAD');
}
/**
* Creates a new GET accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function get(string $path) : RouteBuilder
{
return $this->from($path)->accepting('GET');
}
/**
* Creates a new POST accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function post(string $path) : RouteBuilder
{
return $this->from($path)->accepting('POST');
}
/**
* Creates a new PUT accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function put(string $path) : RouteBuilder
{
return $this->from($path)->accepting('PUT');
}
/**
* Creates a new DELETE accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function delete(string $path) : RouteBuilder
{
return $this->from($path)->accepting('DELETE');
}
/**
* Creates a new OPTIONS accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function options(string $path) : RouteBuilder
{
return $this->from($path)->accepting('OPTIONS');
}
/**
* Creates a new PATCH accepting route.
*
* @param string $path
* @return RouteBuilder
*/
public function patch(string $path) : RouteBuilder
{
return $this->from($path)->accepting('PATCH');
}
/**
* Sets the method(s) which the route should accept.
*
* @param string $method The HTTP method(s) the route should handle
* @return RouteBuilder
*/
public function accepting(string $method) : RouteBuilder
{
$method = strtoupper($method);
$this->methods = array_unique(array_merge($this->methods, [$method]));
return $this;
}
/**
* Removes given method(s) from the set of methods the
* route should handle.
*
* @param string $method The HTTP method(s) the route should no longer handle
* @return RouteBuilder
*/
public function refusing(string $method) : RouteBuilder
{
$method = strtoupper($method);
$this->methods = array_diff($this->methods, [$method]);
return $this;
}
/**
* Sets matcher(s) which the given param should match
* for the route to be active.
*
* @param string $param The param name to set the matcher(s) for
* @param callable $matcher The matcher or array of matchers for the param
* @return RouteBuilder
* @throws \InvalidArgumentException
*/
public function ifMatches(string $param, callable $matcher) : RouteBuilder
{
if (!array_key_exists($param, $this->matchers)) {
$this->matchers[$param] = [];
}
$this->matchers[$param][] = $matcher;
return $this;
}
/**
* Returns a route having removed all formerly set matchers
* for the param with given name.
*
* @param string $param The name of the param all matchers should be removed for
* @return RouteBuilder
*/
public function whateverMatches(string $param) : RouteBuilder
{
if (array_key_exists($param, $this->matchers)) {
unset($this->matchers[$param]);
}
return $this;
}
/**
* Returns the route with a new source configuration.
*
* @param string $path The new path
* @return RouteBuilder
*/
public function from(string $path) : RouteBuilder
{
$this->path = $path;
return $this;
}
/**
* Returns the route with a new target.
*
* @param mixed $target The new target
* @return RouteBuilder
*/
public function to($target) : RouteBuilder
{
$this->target = $target;
return $this;
}
/**
* Returns a new instance of the route carrying the given name.
*
* @param string $name
* @return RouteBuilder
*/
public function named(string $name) : RouteBuilder
{
$this->name = $name;
return $this;
}
/**
* Returns a new instance of the route having the name unset.
*
* @return RouteBuilder
*/
public function noName() : RouteBuilder
{
$this->name = null;
return $this;
}
}