|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
|
19
|
|
|
|
|
20
|
|
|
trait RouteValidation |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Check if given request method matches given route method. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string[]|string $routeMethod |
|
26
|
|
|
* @param string $requestMethod |
|
27
|
|
|
* |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function compareMethod($routeMethod, string $requestMethod): bool |
|
31
|
|
|
{ |
|
32
|
|
|
if (\is_array($routeMethod) && !empty($routeMethod)) { |
|
33
|
|
|
return \in_array($requestMethod, $routeMethod, true); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $routeMethod === $requestMethod; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if given request domain matches given route domain. |
|
41
|
|
|
* |
|
42
|
|
|
* @param null|string $routeDomain |
|
43
|
|
|
* @param string $requestDomain |
|
44
|
|
|
* @param array<int|string,string> $parameters |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return ($routeDomain === null || empty($routeDomain)) || |
|
51
|
|
|
(bool) \preg_match($routeDomain, $requestDomain, $parameters); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check if given request uri matches given uri method. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $routeUri |
|
58
|
|
|
* @param string $requestUri |
|
59
|
|
|
* @param array<int|string,string> $parameters |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function compareUri(string $routeUri, string $requestUri, array &$parameters): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return (bool) \preg_match($routeUri, $requestUri, $parameters); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check if given request uri scheme matches given route scheme. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string[] $routeScheme |
|
72
|
|
|
* @param string $requestScheme |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function compareScheme(array $routeScheme, string $requestScheme): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return empty($routeScheme) || \in_array($requestScheme, $routeScheme, true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get merged default parameters. |
|
83
|
|
|
* |
|
84
|
|
|
* @param array<int|string,mixed> $params |
|
85
|
|
|
* @param array<string,string> $defaults |
|
86
|
|
|
* |
|
87
|
|
|
* @return array<string,string> Merged default parameters |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function mergeDefaults(array $params, array $defaults): array |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($params as $key => $value) { |
|
92
|
|
|
if (!\is_int($key) && (!isset($defaults[$key]) || null !== $value)) { |
|
93
|
|
|
$defaults[$key] = $value; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $defaults; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|