|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\Web; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
|
6
|
|
|
use ValueObjects\Util\Util; |
|
7
|
|
|
use ValueObjects\ValueObjectInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Url implements ValueObjectInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var SchemeName */ |
|
12
|
|
|
protected $scheme; |
|
13
|
|
|
|
|
14
|
|
|
/** @var StringLiteral */ |
|
15
|
|
|
protected $user; |
|
16
|
|
|
|
|
17
|
|
|
/** @var StringLiteral */ |
|
18
|
|
|
protected $password; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Domain */ |
|
21
|
|
|
protected $domain; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Path */ |
|
24
|
|
|
protected $path; |
|
25
|
|
|
|
|
26
|
|
|
/** @var PortNumber */ |
|
27
|
|
|
protected $port; |
|
28
|
|
|
|
|
29
|
|
|
/** @var QueryString */ |
|
30
|
|
|
protected $queryString; |
|
31
|
|
|
|
|
32
|
|
|
/** @var FragmentIdentifier */ |
|
33
|
|
|
protected $fragmentIdentifier; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a new Url object from a native url string |
|
37
|
|
|
* |
|
38
|
|
|
* @param $url_string |
|
39
|
|
|
* @return Url |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public static function fromNative() |
|
42
|
|
|
{ |
|
43
|
2 |
|
$urlString = \func_get_arg(0); |
|
44
|
|
|
|
|
45
|
2 |
|
$user = \parse_url($urlString, PHP_URL_USER); |
|
46
|
2 |
|
$pass = \parse_url($urlString, PHP_URL_PASS); |
|
47
|
2 |
|
$host = \parse_url($urlString, PHP_URL_HOST); |
|
48
|
2 |
|
$queryString = \parse_url($urlString, PHP_URL_QUERY); |
|
49
|
2 |
|
$fragmentId = \parse_url($urlString, PHP_URL_FRAGMENT); |
|
50
|
2 |
|
$port = \parse_url($urlString, PHP_URL_PORT); |
|
51
|
|
|
|
|
52
|
2 |
|
$scheme = new SchemeName(\parse_url($urlString, PHP_URL_SCHEME)); |
|
|
|
|
|
|
53
|
2 |
|
$user = $user ? new StringLiteral($user) : new StringLiteral(''); |
|
54
|
2 |
|
$pass = $pass ? new StringLiteral($pass) : new StringLiteral(''); |
|
55
|
2 |
|
$domain = Domain::specifyType($host); |
|
56
|
2 |
|
$path = new Path(\parse_url($urlString, PHP_URL_PATH)); |
|
57
|
2 |
|
$portNumber = $port ? new PortNumber($port) : new NullPortNumber(); |
|
58
|
2 |
|
$query = $queryString ? new QueryString(\sprintf('?%s', $queryString)) : new NullQueryString(); |
|
59
|
2 |
|
$fragment = $fragmentId ? new FragmentIdentifier(\sprintf('#%s', $fragmentId)) : new NullFragmentIdentifier(); |
|
60
|
|
|
|
|
61
|
2 |
|
return new static($scheme, $user, $pass, $domain, $portNumber, $path, $query, $fragment); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns a new Url object |
|
66
|
|
|
* |
|
67
|
|
|
* @param SchemeName $scheme |
|
68
|
|
|
* @param StringLiteral $user |
|
69
|
|
|
* @param StringLiteral $password |
|
70
|
|
|
* @param Domain $domain |
|
71
|
|
|
* @param Path $path |
|
72
|
|
|
* @param PortNumberInterface $port |
|
73
|
|
|
* @param QueryString $query |
|
74
|
|
|
* @param FragmentIdentifier $fragment |
|
75
|
|
|
*/ |
|
76
|
13 |
|
public function __construct(SchemeName $scheme, StringLiteral $user, StringLiteral $password, Domain $domain, PortNumberInterface $port, Path $path, QueryString $query, FragmentIdentifier $fragment) |
|
77
|
|
|
{ |
|
78
|
13 |
|
$this->scheme = $scheme; |
|
79
|
13 |
|
$this->user = $user; |
|
80
|
13 |
|
$this->password = $password; |
|
81
|
13 |
|
$this->domain = $domain; |
|
82
|
13 |
|
$this->path = $path; |
|
83
|
13 |
|
$this->port = $port; |
|
|
|
|
|
|
84
|
13 |
|
$this->queryString = $query; |
|
85
|
13 |
|
$this->fragmentIdentifier = $fragment; |
|
86
|
13 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Tells whether two Url are sameValueAs by comparing their components |
|
90
|
|
|
* |
|
91
|
|
|
* @param ValueObjectInterface $url |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function sameValueAs(ValueObjectInterface $url) |
|
95
|
|
|
{ |
|
96
|
2 |
|
if (false === Util::classEquals($this, $url)) { |
|
97
|
1 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return $this->getScheme()->sameValueAs($url->getScheme()) && |
|
|
|
|
|
|
101
|
2 |
|
$this->getUser()->sameValueAs($url->getUser()) && |
|
|
|
|
|
|
102
|
2 |
|
$this->getPassword()->sameValueAs($url->getPassword()) && |
|
|
|
|
|
|
103
|
2 |
|
$this->getDomain()->sameValueAs($url->getDomain()) && |
|
|
|
|
|
|
104
|
2 |
|
$this->getPath()->sameValueAs($url->getPath()) && |
|
|
|
|
|
|
105
|
2 |
|
$this->getPort()->sameValueAs($url->getPort()) && |
|
|
|
|
|
|
106
|
2 |
|
$this->getQueryString()->sameValueAs($url->getQueryString()) && |
|
|
|
|
|
|
107
|
2 |
|
$this->getFragmentIdentifier()->sameValueAs($url->getFragmentIdentifier()) |
|
|
|
|
|
|
108
|
|
|
; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the domain of the Url |
|
113
|
|
|
* |
|
114
|
|
|
* @return Hostname|IPAddress |
|
115
|
|
|
*/ |
|
116
|
6 |
|
public function getDomain() |
|
117
|
|
|
{ |
|
118
|
6 |
|
return clone $this->domain; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the fragment identifier of the Url |
|
123
|
|
|
* |
|
124
|
|
|
* @return FragmentIdentifier |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function getFragmentIdentifier() |
|
127
|
|
|
{ |
|
128
|
6 |
|
return clone $this->fragmentIdentifier; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the password part of the Url |
|
133
|
|
|
* |
|
134
|
|
|
* @return StringLiteral |
|
135
|
|
|
*/ |
|
136
|
5 |
|
public function getPassword() |
|
137
|
|
|
{ |
|
138
|
5 |
|
return clone $this->password; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the path of the Url |
|
143
|
|
|
* |
|
144
|
|
|
* @return Path |
|
145
|
|
|
*/ |
|
146
|
6 |
|
public function getPath() |
|
147
|
|
|
{ |
|
148
|
6 |
|
return clone $this->path; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the port of the Url |
|
153
|
|
|
* |
|
154
|
|
|
* @return PortNumberInterface |
|
155
|
|
|
*/ |
|
156
|
6 |
|
public function getPort() |
|
157
|
|
|
{ |
|
158
|
6 |
|
return clone $this->port; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns the query string of the Url |
|
163
|
|
|
* |
|
164
|
|
|
* @return QueryString |
|
165
|
|
|
*/ |
|
166
|
6 |
|
public function getQueryString() |
|
167
|
|
|
{ |
|
168
|
6 |
|
return clone $this->queryString; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the scheme of the Url |
|
173
|
|
|
* |
|
174
|
|
|
* @return SchemeName |
|
175
|
|
|
*/ |
|
176
|
6 |
|
public function getScheme() |
|
177
|
|
|
{ |
|
178
|
6 |
|
return clone $this->scheme; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the user part of the Url |
|
183
|
|
|
* |
|
184
|
|
|
* @return StringLiteral |
|
185
|
|
|
*/ |
|
186
|
6 |
|
public function getUser() |
|
187
|
|
|
{ |
|
188
|
6 |
|
return clone $this->user; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Returns a string representation of the url |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
4 |
|
public function __toString() |
|
197
|
|
|
{ |
|
198
|
4 |
|
$userPass = ''; |
|
199
|
4 |
|
if (false === $this->getUser()->isEmpty()) { |
|
200
|
2 |
|
$userPass = \sprintf('%s@', $this->getUser()); |
|
201
|
|
|
|
|
202
|
2 |
|
if (false === $this->getPassword()->isEmpty()) { |
|
203
|
1 |
|
$userPass = \sprintf('%s:%s@', $this->getUser(), $this->getPassword()); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
4 |
|
$port = ''; |
|
208
|
4 |
|
if (false === NullPortNumber::create()->sameValueAs($this->getPort())) { |
|
209
|
2 |
|
$port = \sprintf(':%d', $this->getPort()->toNative()); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
4 |
|
$urlString = \sprintf('%s://%s%s%s%s%s%s', $this->getScheme(), $userPass, $this->getDomain(), $port, $this->getPath(), $this->getQueryString(), $this->getFragmentIdentifier()); |
|
213
|
|
|
|
|
214
|
4 |
|
return $urlString; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
218
|
|
|
{ |
|
219
|
|
|
return [ |
|
220
|
|
|
'url' => (string) $this, |
|
221
|
|
|
'scheme' => $this->getScheme(), |
|
222
|
|
|
'user' => $this->getUser(), |
|
223
|
|
|
'password' => $this->getPassword(), |
|
224
|
|
|
'domain' => $this->getDomain(), |
|
225
|
|
|
'port' => $this->getPort(), |
|
226
|
|
|
'path' => $this->getPath(), |
|
227
|
|
|
'query' => $this->getQueryString(), |
|
228
|
|
|
'fragment' => $this->getFragmentIdentifier() |
|
229
|
|
|
]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|