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\Traits; |
19
|
|
|
|
20
|
|
|
use Flight\Routing\Exceptions\MethodNotAllowedException; |
21
|
|
|
use Flight\Routing\Exceptions\UriHandlerException; |
22
|
|
|
use Flight\Routing\Route; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
24
|
|
|
use Psr\Http\Message\UriInterface; |
25
|
|
|
|
26
|
|
|
trait ValidationTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Check if given request domain matches given route domain. |
30
|
|
|
* |
31
|
|
|
* @param string[] $routeDomains |
32
|
|
|
* @param array<int|string,string> $parameters |
33
|
|
|
*/ |
34
|
|
|
protected function compareDomain(array $routeDomains, string $requestDomain, array &$parameters): bool |
35
|
48 |
|
{ |
36
|
|
|
foreach ($routeDomains as $routeDomain) { |
37
|
48 |
|
if (1 === \preg_match($routeDomain, $requestDomain, $parameters)) { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return empty($routeDomains); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Check if given request uri matches given uri method. |
47
|
|
|
* |
48
|
|
|
* @param array<int|string,string> $parameters |
49
|
9 |
|
*/ |
50
|
|
|
protected function compareUri(string $routeUri, string $requestUri, array &$parameters): bool |
51
|
9 |
|
{ |
52
|
9 |
|
return 1 === \preg_match($routeUri, $requestUri, $parameters); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Asserts the Route's method and domain scheme. |
57
|
|
|
*/ |
58
|
|
|
protected function assertRoute(Route $route, UriInterface $requestUri, string $method): void |
59
|
|
|
{ |
60
|
|
|
/** @var string[] $methods */ |
61
|
|
|
$methods = $route->get('methods'); |
62
|
|
|
|
63
|
|
|
// Check if given request method matches given route method. |
64
|
9 |
|
if (!isset($methods[$method])) { |
65
|
|
|
throw new MethodNotAllowedException(\array_keys($methods), $requestUri->getPath(), $method); |
66
|
9 |
|
} |
67
|
|
|
|
68
|
|
|
/** @var string[] $schemes */ |
69
|
|
|
$schemes = $route->get('schemes'); |
70
|
|
|
$scheme = $requestUri->getScheme(); |
71
|
|
|
|
72
|
|
|
// Check if given request uri scheme matches given route scheme. |
73
|
|
|
if (!(empty($schemes) || isset($schemes[$scheme]))) { |
74
|
|
|
throw new UriHandlerException( |
75
|
|
|
\sprintf( |
76
|
|
|
'Unfortunately current scheme "%s" is not allowed on requested uri [%s]', |
77
|
46 |
|
$requestUri->getScheme(), |
78
|
|
|
$requestUri->getPath() |
79
|
46 |
|
), |
80
|
|
|
400 |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Asserts the Route's host and port |
87
|
|
|
*/ |
88
|
|
|
protected function assertHost(string $hostAndPort, string $requestPath): UriHandlerException |
89
|
48 |
|
{ |
90
|
|
|
return new UriHandlerException( |
91
|
48 |
|
\sprintf( |
92
|
2 |
|
'Unfortunately current domain "%s" is not allowed on requested uri [%s]', |
93
|
|
|
$hostAndPort, |
94
|
|
|
$requestPath |
95
|
46 |
|
), |
96
|
1 |
|
400 |
97
|
1 |
|
); |
98
|
1 |
|
} |
99
|
1 |
|
|
100
|
1 |
|
/** |
101
|
|
|
* Resolve request path to match sub-directory, server, and domain paths. |
102
|
1 |
|
*/ |
103
|
|
|
protected function resolvePath(ServerRequestInterface $request): string |
104
|
|
|
{ |
105
|
45 |
|
$requestPath = $request->getUri()->getPath(); |
106
|
|
|
$basePath = $request->getServerParams()['SCRIPT_NAME'] ?? ''; |
107
|
|
|
|
108
|
|
|
if ( |
109
|
|
|
$basePath !== $requestPath && |
110
|
|
|
\strlen($basePath = \dirname($basePath)) > 1 && |
111
|
|
|
$basePath !== '/index.php' |
112
|
|
|
) { |
113
|
|
|
$requestPath = \substr($requestPath, \strcmp($basePath, $requestPath)) ?: ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return \strlen($requestPath) > 1 ? \rtrim($requestPath, '/') : $requestPath; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|