1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequestPsr\Content; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Address |
12
|
|
|
* @package kalanis\RemoteRequestPsr\Content |
13
|
|
|
* Simple implementation of URI interface as address |
14
|
|
|
*/ |
15
|
|
|
class Address implements UriInterface |
16
|
|
|
{ |
17
|
|
|
protected string $scheme = ''; |
18
|
|
|
protected string $target = ''; |
19
|
|
|
protected string $userInfo = ''; |
20
|
|
|
protected string $host = ''; |
21
|
|
|
protected int $port = 0; |
22
|
|
|
protected string $path = '/'; |
23
|
|
|
protected string $query = ''; |
24
|
|
|
protected string $fragment = ''; |
25
|
|
|
protected string $user = ''; |
26
|
|
|
protected ?string $pass = null; |
27
|
|
|
|
28
|
5 |
|
public function getScheme(): string |
29
|
|
|
{ |
30
|
5 |
|
return $this->scheme; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function getAuthority(): string |
34
|
|
|
{ |
35
|
2 |
|
return empty($this->getHost()) |
36
|
1 |
|
? '' |
37
|
|
|
: ( |
38
|
1 |
|
(!empty($this->getUserInfo()) ? $this->getUserInfo() . '@' : '') |
39
|
1 |
|
. $this->getHost() |
40
|
2 |
|
. (empty($this->getPort()) || (80 == $this->getPort()) ? '' : ':' . $this->getPort()) |
41
|
|
|
) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
public function getUserInfo(): string |
46
|
|
|
{ |
47
|
5 |
|
return strval($this->user) . |
48
|
5 |
|
(empty($this->pass) |
49
|
4 |
|
? '' |
50
|
5 |
|
: ':' . strval($this->pass)); |
51
|
|
|
} |
52
|
|
|
|
53
|
5 |
|
public function getHost(): string |
54
|
|
|
{ |
55
|
5 |
|
return $this->host; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
public function getPort(): ?int |
59
|
|
|
{ |
60
|
5 |
|
if (!empty($this->port)) { |
61
|
2 |
|
return $this->port; |
62
|
|
|
} |
63
|
4 |
|
if (empty($this->scheme)) { |
64
|
3 |
|
return null; |
65
|
|
|
} |
66
|
2 |
|
return 80; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function getPath(): string |
70
|
|
|
{ |
71
|
2 |
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function getQuery(): string |
75
|
|
|
{ |
76
|
2 |
|
return $this->query; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getFragment(): string |
80
|
|
|
{ |
81
|
2 |
|
return $this->fragment; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function withScheme(string $scheme): UriInterface |
85
|
|
|
{ |
86
|
3 |
|
$this->scheme = $scheme; |
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function withUserInfo(string $user, ?string $password = null): UriInterface |
91
|
|
|
{ |
92
|
2 |
|
$this->user = $user; |
93
|
2 |
|
$this->pass = $password; |
94
|
2 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
5 |
|
public function withHost(string $host): UriInterface |
98
|
|
|
{ |
99
|
5 |
|
$this->host = $host; |
100
|
5 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
public function withPort(?int $port): UriInterface |
104
|
|
|
{ |
105
|
4 |
|
if (is_null($port)) { |
106
|
1 |
|
$this->port = 0; |
107
|
|
|
} else { |
108
|
4 |
|
if (65535 < $port) { |
109
|
1 |
|
throw new InvalidArgumentException('Port number is too large'); |
110
|
3 |
|
} elseif (0 > $port) { |
111
|
1 |
|
throw new InvalidArgumentException('Port number is too low'); |
112
|
|
|
} |
113
|
2 |
|
$this->port = $port; |
114
|
|
|
} |
115
|
2 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
public function withPath(string $path): UriInterface |
119
|
|
|
{ |
120
|
4 |
|
$this->path = empty($path) ? '/' : $path; |
121
|
4 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function withQuery(string $query): UriInterface |
125
|
|
|
{ |
126
|
2 |
|
$this->query = $query; |
127
|
2 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function withFragment(string $fragment): UriInterface |
131
|
|
|
{ |
132
|
1 |
|
$this->fragment = $fragment; |
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function __toString(): string |
137
|
|
|
{ |
138
|
1 |
|
$sch = $this->getScheme(); |
139
|
1 |
|
$auth = $this->getAuthority(); |
140
|
1 |
|
$pt = $this->getPath(); |
141
|
1 |
|
$q = $this->getQuery(); |
142
|
1 |
|
$fr = $this->getFragment(); |
143
|
|
|
return |
144
|
1 |
|
(empty($sch) ? '' : $sch . ':') |
145
|
1 |
|
. (empty($auth) ? '' : '//' . $auth) |
146
|
1 |
|
. ((!$this->detectRoot($pt) && !empty($auth)) ? '/' . $pt : $pt) |
147
|
1 |
|
. (empty($q) ? '' : '?' . $q) |
148
|
1 |
|
. (empty($fr) ? '' : '#' . $fr) |
149
|
|
|
; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
protected function detectRoot(string $path): bool |
153
|
|
|
{ |
154
|
1 |
|
return isset($path[0]) && ('/' == $path[0]); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|