|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 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
|
|
|
use Flight\Routing\Exceptions\UrlGenerationException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A generated URI from route made up of only the |
|
24
|
|
|
* URIs path component (pathinfo, scheme, host, and maybe port.) starting with a slash. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class RouteUri implements \Stringable |
|
29
|
|
|
{ |
|
30
|
|
|
/** Generates an absolute URL, e.g. "http://example.com/dir/file". */ |
|
31
|
|
|
public const ABSOLUTE_URL = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** Generates an absolute path, e.g. "/dir/file". */ |
|
34
|
|
|
public const ABSOLUTE_PATH = 1; |
|
35
|
|
|
|
|
36
|
|
|
/** Generates a path with beginning with a single dot, e.g. "./file". */ |
|
37
|
|
|
public const RELATIVE_PATH = 2; |
|
38
|
|
|
|
|
39
|
|
|
/** Generates a network path, e.g. "//example.com/dir/file". */ |
|
40
|
|
|
public const NETWORK_PATH = 3; |
|
41
|
|
|
|
|
42
|
|
|
/** Adopted from symfony's routing component: Symfony\Component\Routing\Generator::QUERY_FRAGMENT_DECODED */ |
|
43
|
|
|
private const QUERY_DECODED = [ |
|
44
|
|
|
// RFC 3986 explicitly allows those in the query to reference other URIs unencoded |
|
45
|
|
|
'%2F' => '/', |
|
46
|
|
|
'%3F' => '?', |
|
47
|
|
|
// reserved chars that have no special meaning for HTTP URIs in a query |
|
48
|
|
|
// this excludes esp. "&", "=" and also "+" because PHP would treat it as a space (form-encoded) |
|
49
|
|
|
'%40' => '@', |
|
50
|
|
|
'%3A' => ':', |
|
51
|
|
|
'%21' => '!', |
|
52
|
|
|
'%3B' => ';', |
|
53
|
|
|
'%2C' => ',', |
|
54
|
|
|
'%2A' => '*', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
private string $pathInfo; |
|
58
|
|
|
private int $referenceType; |
|
59
|
|
|
private ?string $scheme = null, $host = null, $port = null; |
|
60
|
|
|
|
|
61
|
|
|
public function __construct(string $pathInfo, int $referenceType) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->pathInfo = $pathInfo; |
|
64
|
|
|
$this->referenceType = $referenceType; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __toString() |
|
71
|
|
|
{ |
|
72
|
|
|
$pathInfo = '/'.\ltrim($this->pathInfo, '/'); |
|
73
|
|
|
|
|
74
|
|
|
if (self::ABSOLUTE_PATH === $type = $this->referenceType) { |
|
75
|
|
|
return $pathInfo; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (self::RELATIVE_PATH === $type) { |
|
79
|
|
|
return '.'.$pathInfo; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (isset($this->host)) { |
|
83
|
|
|
$hostPort = $this->host.$this->port; |
|
84
|
|
|
} else { |
|
85
|
|
|
$h = \explode(':', $_SERVER['HTTP_HOST'] ?? 'localhost:80', 2); |
|
86
|
|
|
$hostPort = $h[0].($this->port ?? (!\in_array($h[1] ?? '', ['', '80', '443'], true) ? ':'.$h[0] : '')); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return (self::NETWORK_PATH === $type ? '//' : (isset($this->scheme) ? $this->scheme.'://' : '//')).$hostPort.$pathInfo; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set the host component of the URI, may include port too. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function withHost(string $host): self |
|
96
|
|
|
{ |
|
97
|
|
|
$this->host = $host; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set the scheme component of the URI. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function withScheme(string $scheme): self |
|
106
|
|
|
{ |
|
107
|
|
|
$this->scheme = $scheme; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Sets the port component of the URI. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function withPort(int $port): self |
|
116
|
|
|
{ |
|
117
|
|
|
if (0 > $port || 0xFFFF < $port) { |
|
118
|
|
|
throw new UrlGenerationException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!\in_array($port, [80, 443], true)) { |
|
122
|
|
|
$this->port = ':'.$port; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the query component of the URI. |
|
130
|
|
|
* |
|
131
|
|
|
* @param array<int|string,int|string> $queryParams |
|
132
|
|
|
*/ |
|
133
|
|
|
public function withQuery(array $queryParams = []): self |
|
134
|
|
|
{ |
|
135
|
|
|
// Incase query is added to uri. |
|
136
|
|
|
if ([] !== $queryParams) { |
|
137
|
|
|
$queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986); |
|
138
|
|
|
|
|
139
|
|
|
if (!empty($queryString)) { |
|
140
|
|
|
$this->pathInfo .= '?'.\strtr($queryString, self::QUERY_DECODED); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set the fragment component of the URI. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function withFragment(string $fragment): self |
|
151
|
|
|
{ |
|
152
|
|
|
if (!empty($fragment)) { |
|
153
|
|
|
$this->pathInfo .= '#'.$fragment; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|