1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Application Utils |
4
|
|
|
* @subpackage URLInfo |
5
|
|
|
* @see \AppUtils\URLInfo\URLInfoTrait |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace AppUtils\URLInfo; |
11
|
|
|
|
12
|
|
|
use AppUtils\URLInfo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait used for classes that access a URL info array, |
16
|
|
|
* as parsed using the parse_url method. |
17
|
|
|
* |
18
|
|
|
* @package Application Utils |
19
|
|
|
* @subpackage URLInfo |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
trait URLInfoTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array<string,mixed> |
26
|
|
|
*/ |
27
|
|
|
protected array $info = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array<string,mixed> |
31
|
|
|
*/ |
32
|
|
|
public function getInfo() : array |
33
|
|
|
{ |
34
|
|
|
return $this->info; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getKey(string $name) : ?string |
38
|
|
|
{ |
39
|
|
|
return $this->info[$name] ?? null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getKeyString(string $name) : string |
43
|
|
|
{ |
44
|
|
|
return (string)$this->getKey($name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getHost() : string |
48
|
|
|
{ |
49
|
|
|
return $this->getKeyString('host'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getType() : string |
53
|
|
|
{ |
54
|
|
|
return $this->getKeyString('type'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getUser() : string |
58
|
|
|
{ |
59
|
|
|
return $this->getKeyString('user'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getPassword() : string |
63
|
|
|
{ |
64
|
|
|
return $this->getKeyString('pass'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getQuery() : string |
68
|
|
|
{ |
69
|
|
|
return $this->getKeyString('query'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getPath() : string |
73
|
|
|
{ |
74
|
|
|
return$this->getKeyString('path'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getScheme() : string |
78
|
|
|
{ |
79
|
|
|
return $this->getKeyString('scheme'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setHost(string $host) : self |
83
|
|
|
{ |
84
|
|
|
return $this->setKey('host', $host); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setHostFromEmail(string $email) : self |
88
|
|
|
{ |
89
|
|
|
$parts = explode('@', $email); |
90
|
|
|
$this->setHost(array_pop($parts)); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setAuth(string $user, string $password) : self |
96
|
|
|
{ |
97
|
|
|
$this->setKey('user', $user); |
98
|
|
|
$this->setKey('pass', $password); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setPath(string $path) : self |
104
|
|
|
{ |
105
|
|
|
return $this->setKey('path', $path); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setKey(string $name, string $value) : self |
109
|
|
|
{ |
110
|
|
|
$this->info[$name] = $value; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setIP(string $ip) : self |
115
|
|
|
{ |
116
|
|
|
$this->setHost($ip); |
117
|
|
|
$this->setKey('ip', $ip); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setSchemeHTTPS() : self |
123
|
|
|
{ |
124
|
|
|
return $this->setScheme('https'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setSchemeMailto() : self |
128
|
|
|
{ |
129
|
|
|
return $this->setScheme('mailto'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setScheme(string $scheme) : self |
133
|
|
|
{ |
134
|
|
|
return $this->setKey('scheme', $scheme); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setTypeURL() : self |
138
|
|
|
{ |
139
|
|
|
return $this->setType(URLInfo::TYPE_URL); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setTypeEmail() : void |
143
|
|
|
{ |
144
|
|
|
$this->setType(URLInfo::TYPE_EMAIL); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $type |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function setType(string $type) : self |
152
|
|
|
{ |
153
|
|
|
$this->info['type'] = $type; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function removePath() : self |
158
|
|
|
{ |
159
|
|
|
return $this->removeKey('path'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function removeKey(string $name) : self |
163
|
|
|
{ |
164
|
|
|
unset($this->info[$name]); |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function hasAuth() : bool |
169
|
|
|
{ |
170
|
|
|
return isset($this->info['user'], $this->info['pass']); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function hasKey(string $name) : bool |
174
|
|
|
{ |
175
|
|
|
return isset($this->info[$name]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function hasHost() : bool |
179
|
|
|
{ |
180
|
|
|
return $this->hasKey('host'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function hasQuery() : bool |
184
|
|
|
{ |
185
|
|
|
return $this->hasKey('query'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function hasScheme() : bool |
189
|
|
|
{ |
190
|
|
|
return $this->hasKey('scheme'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function hasPath() : bool |
194
|
|
|
{ |
195
|
|
|
return $this->hasKey('path'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function hasFragment() : bool |
199
|
|
|
{ |
200
|
|
|
return $this->hasKey('fragment'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function isFragmentOnly() : bool |
204
|
|
|
{ |
205
|
|
|
return |
206
|
|
|
$this->hasFragment() |
207
|
|
|
&& |
208
|
|
|
( |
209
|
|
|
!$this->hasScheme() |
210
|
|
|
&& |
211
|
|
|
!$this->hasHost() |
212
|
|
|
&& |
213
|
|
|
!$this->hasPath() |
214
|
|
|
&& |
215
|
|
|
!$this->hasQuery() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function isPathOnly() : bool |
220
|
|
|
{ |
221
|
|
|
return |
222
|
|
|
$this->hasPath() |
223
|
|
|
&& |
224
|
|
|
( |
225
|
|
|
!$this->hasScheme() |
226
|
|
|
&& |
227
|
|
|
!$this->hasHost() |
228
|
|
|
&& |
229
|
|
|
!$this->hasQuery() |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function isHostOnly() : bool |
234
|
|
|
{ |
235
|
|
|
return |
236
|
|
|
$this->hasHost() |
237
|
|
|
&& |
238
|
|
|
( |
239
|
|
|
!$this->hasScheme() |
240
|
|
|
&& |
241
|
|
|
!$this->hasPath() |
242
|
|
|
&& |
243
|
|
|
!$this->hasQuery() |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function isSchemeLess() : bool |
248
|
|
|
{ |
249
|
|
|
return $this->isFragmentOnly(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|