Passed
Push — master ( 71cd2b...ebd1fd )
by Melech
04:05
created

Routable::withMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Routing\Models;
15
16
use InvalidArgumentException;
17
use Valkyrja\Http\Constants\RequestMethod;
18
use Valkyrja\Http\Constants\StatusCode;
19
use Valkyrja\Support\Type\Str;
20
21
use function array_diff;
22
23
/**
24
 * Trait Routable.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
trait Routable
29
{
30
    /**
31
     * The path for this route.
32
     *
33
     * @var string|null
34
     */
35
    public ?string $path = null;
36
37
    /**
38
     * The redirect path for this route.
39
     *
40
     * @var string|null
41
     */
42
    public ?string $to = null;
43
44
    /**
45
     * The redirect status code for this route.
46
     *
47
     * @var int
48
     */
49
    public int $code = StatusCode::FOUND;
50
51
    /**
52
     * The request methods for this route.
53
     *
54
     * @var array
55
     */
56
    public array $methods = [
57
        RequestMethod::GET,
58
        RequestMethod::HEAD,
59
    ];
60
61
    /**
62
     * The regex for dynamic routes.
63
     *
64
     * @var string|null
65
     */
66
    public ?string $regex = null;
67
68
    /**
69
     * Any params for dynamic routes.
70
     *
71
     * @var array|null
72
     */
73
    public ?array $params = null;
74
75
    /**
76
     * Any segments for optional parts of path.
77
     *
78
     * @var array|null
79
     */
80
    public ?array $segments = null;
81
82
    /**
83
     * The middleware for this route.
84
     *
85
     * @var array|null
86
     */
87
    public ?array $middleware = null;
88
89
    /**
90
     * Whether the route is dynamic.
91
     *
92
     * @var bool
93
     */
94
    public bool $dynamic = false;
95
96
    /**
97
     * Whether the route is secure.
98
     *
99
     * @var bool
100
     */
101
    public bool $secure = false;
102
103
    /**
104
     * Whether the route is a redirect.
105
     *
106
     * @var bool
107
     */
108
    public bool $redirect = false;
109
110
    /**
111
     * Get the route's path.
112
     *
113
     * @return string|null
114
     */
115
    public function getPath(): ?string
116
    {
117
        return $this->path;
118
    }
119
120
    /**
121
     * Set the route's path.
122
     *
123
     * @param string $path The route path
124
     *
125
     * @return static
126
     */
127
    public function setPath(string $path): self
128
    {
129
        $this->dynamic = Str::contains($path, '{');
130
131
        $this->path = $path;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Get the redirect path.
138
     *
139
     * @return string|null
140
     */
141
    public function getTo(): ?string
142
    {
143
        return $this->to;
144
    }
145
146
    /**
147
     * Set the redirect path.
148
     *
149
     * @param string|null $to
150
     *
151
     * @return static
152
     */
153
    public function setTo(string $to = null): self
154
    {
155
        $this->to = $to;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Get the redirect status code.
162
     *
163
     * @return int
164
     */
165
    public function getCode(): int
166
    {
167
        return $this->code;
168
    }
169
170
    /**
171
     * Set the redirect status code.
172
     *
173
     * @param int $code
174
     *
175
     * @return static
176
     */
177
    public function setCode(int $code): self
178
    {
179
        $this->code = $code;
180
181
        return $this;
182
    }
183
184
    /**
185
     * Get the request methods.
186
     *
187
     * @return array
188
     */
189
    public function getMethods(): array
190
    {
191
        return $this->methods;
192
    }
193
194
    /**
195
     * Set the request methods.
196
     *
197
     * @param array $methods The request methods
198
     *
199
     * @throws InvalidArgumentException
200
     *
201
     * @return static
202
     */
203
    public function setMethods(array $methods): self
204
    {
205
        if (array_diff($methods, RequestMethod::ANY)) {
206
            throw new InvalidArgumentException('Invalid request methods set');
207
        }
208
209
        $this->methods = $methods;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Get the regex.
216
     *
217
     * @return string|null
218
     */
219
    public function getRegex(): ?string
220
    {
221
        return $this->regex;
222
    }
223
224
    /**
225
     * Set the regex.
226
     *
227
     * @param string|null $regex The regex
228
     *
229
     * @return static
230
     */
231
    public function setRegex(string $regex = null): self
232
    {
233
        $this->regex = $regex;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Get the params.
240
     *
241
     * @return array|null
242
     */
243
    public function getParams(): ?array
244
    {
245
        return $this->params;
246
    }
247
248
    /**
249
     * Set the params.
250
     *
251
     * @param array|null $params The params
252
     *
253
     * @return static
254
     */
255
    public function setParams(array $params = null): self
256
    {
257
        $this->params = $params;
258
259
        return $this;
260
    }
261
262
    /**
263
     * Get the segments.
264
     *
265
     * @return array|null
266
     */
267
    public function getSegments(): ?array
268
    {
269
        return $this->segments;
270
    }
271
272
    /**
273
     * Set the segments.
274
     *
275
     * @param array|null $segments The segments
276
     *
277
     * @return static
278
     */
279
    public function setSegments(array $segments = null): self
280
    {
281
        $this->segments = $segments;
282
283
        return $this;
284
    }
285
286
    /**
287
     * Get the middleware.
288
     *
289
     * @return array|null
290
     */
291
    public function getMiddleware(): ?array
292
    {
293
        return $this->middleware;
294
    }
295
296
    /**
297
     * Set the middleware.
298
     *
299
     * @param array|null $middleware The middleware
300
     *
301
     * @return static
302
     */
303
    public function setMiddleware(array $middleware = null): self
304
    {
305
        $this->middleware = $middleware;
306
307
        return $this;
308
    }
309
310
    /**
311
     * Route with added middleware.
312
     *
313
     * @param array $middleware The middleware
314
     *
315
     * @return static
316
     */
317
    public function withMiddleware(array $middleware): self
318
    {
319
        $this->middleware = array_merge($this->middleware ?? [], $middleware);
320
321
        return $this;
322
    }
323
324
    /**
325
     * Check whether the route is dynamic.
326
     *
327
     * @return bool
328
     */
329
    public function isDynamic(): bool
330
    {
331
        return $this->dynamic;
332
    }
333
334
    /**
335
     * Set the route as dynamic.
336
     *
337
     * @param bool $dynamic Whether the route it dynamic
338
     *
339
     * @return static
340
     */
341
    public function setDynamic(bool $dynamic = true): self
342
    {
343
        $this->dynamic = $dynamic;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Get whether the route is secure.
350
     *
351
     * @return bool
352
     */
353
    public function isSecure(): bool
354
    {
355
        return $this->secure;
356
    }
357
358
    /**
359
     * Set whether the route is secure.
360
     *
361
     * @param bool $secure Whether the route is secure
362
     *
363
     * @return static
364
     */
365
    public function setSecure(bool $secure = true): self
366
    {
367
        $this->secure = $secure;
368
369
        return $this;
370
    }
371
372
    /**
373
     * Get whether the route is a redirect.
374
     *
375
     * @return bool
376
     */
377
    public function isRedirect(): bool
378
    {
379
        return $this->redirect;
380
    }
381
382
    /**
383
     * Set whether the route is a redirect.
384
     *
385
     * @param bool $redirect
386
     *
387
     * @return static
388
     */
389
    public function setRedirect(bool $redirect): self
390
    {
391
        $this->redirect = $redirect;
392
393
        return $this;
394
    }
395
}
396