-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUrl.php
More file actions
256 lines (227 loc) · 7.1 KB
/
Url.php
File metadata and controls
256 lines (227 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
declare(strict_types=1);
/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
* Copyright (c) 2018 Yuuki Takezawa
*/
namespace ValueObjects\Web;
use ValueObjects\StringLiteral\StringLiteral;
use ValueObjects\Util\Util;
use ValueObjects\ValueObjectInterface;
/**
* Class Url.
*/
class Url implements ValueObjectInterface
{
/** @var SchemeName */
protected $scheme;
/** @var StringLiteral */
protected $user;
/** @var StringLiteral */
protected $password;
/** @var Domain */
protected $domain;
/** @var Path */
protected $path;
/** @var PortNumber */
protected $port;
/** @var QueryString */
protected $queryString;
/** @var FragmentIdentifier */
protected $fragmentIdentifier;
/**
* Returns a new Url object.
*
* @param SchemeName $scheme
* @param StringLiteral $user
* @param StringLiteral $password
* @param Domain $domain
* @param Path $path
* @param PortNumberInterface $port
* @param QueryString $query
* @param FragmentIdentifier $fragment
*/
public function __construct(
SchemeName $scheme,
StringLiteral $user,
StringLiteral $password,
Domain $domain,
PortNumberInterface $port,
Path $path,
QueryString $query,
FragmentIdentifier $fragment
) {
$this->scheme = $scheme;
$this->user = $user;
$this->password = $password;
$this->domain = $domain;
$this->path = $path;
$this->port = $port;
$this->queryString = $query;
$this->fragmentIdentifier = $fragment;
}
/**
* Returns a string representation of the url.
*
* @return string
*/
public function __toString(): string
{
$userPass = '';
if (false === $this->getUser()->isEmpty()) {
$userPass = \sprintf('%s@', $this->getUser());
if (false === $this->getPassword()->isEmpty()) {
$userPass = \sprintf('%s:%s@', $this->getUser(), $this->getPassword());
}
}
$port = '';
if (false === NullPortNumber::create()->sameValueAs($this->getPort())) {
$port = \sprintf(':%d', $this->getPort()->toNative());
}
return \sprintf(
'%s://%s%s%s%s%s%s',
$this->getScheme(),
$userPass,
$this->getDomain(),
$port,
$this->getPath(),
$this->getQueryString(),
$this->getFragmentIdentifier()
);
}
/**
* Returns a new Url object from a native url string.
*
* @param ...string $url
*
* @return Url|ValueObjectInterface
*/
public static function fromNative(): ValueObjectInterface
{
$unsanitisedUrl = \strval(\func_get_arg(0));
$urlString = filter_var($unsanitisedUrl, FILTER_VALIDATE_URL);
if (false === $urlString) {
throw new \InvalidArgumentException("Invalid URL : " . $unsanitisedUrl);
}
$user = \parse_url($urlString, PHP_URL_USER);
$pass = \parse_url($urlString, PHP_URL_PASS);
$host = \parse_url($urlString, PHP_URL_HOST);
$queryString = \parse_url($urlString, PHP_URL_QUERY);
$fragmentId = \parse_url($urlString, PHP_URL_FRAGMENT);
$port = \parse_url($urlString, PHP_URL_PORT);
$scheme = new SchemeName(\parse_url($urlString, PHP_URL_SCHEME));
$user = $user ? new StringLiteral($user) : new StringLiteral('');
$pass = $pass ? new StringLiteral($pass) : new StringLiteral('');
$domain = Domain::specifyType($host);
$path = \parse_url($urlString, PHP_URL_PATH);
if (is_null($path)) {
$path = '';
}
$path = new Path($path);
$portNumber = $port ? new PortNumber($port) : new NullPortNumber();
$query = $queryString ? new QueryString(\sprintf('?%s', $queryString)) : new NullQueryString();
$fragment = $fragmentId ? new FragmentIdentifier(\sprintf('#%s', $fragmentId)) : new NullFragmentIdentifier();
return new static($scheme, $user, $pass, $domain, $portNumber, $path, $query, $fragment);
}
/**
* Tells whether two Url are sameValueAs by comparing their components.
*
* @param Url|ValueObjectInterface $url
*
* @return bool
*/
public function sameValueAs(ValueObjectInterface $url): bool
{
if (false === Util::classEquals($this, $url)) {
return false;
}
return $this->getScheme()->sameValueAs($url->getScheme()) &&
$this->getUser()->sameValueAs($url->getUser()) &&
$this->getPassword()->sameValueAs($url->getPassword()) &&
$this->getDomain()->sameValueAs($url->getDomain()) &&
$this->getPath()->sameValueAs($url->getPath()) &&
$this->getPort()->sameValueAs($url->getPort()) &&
$this->getQueryString()->sameValueAs($url->getQueryString()) &&
$this->getFragmentIdentifier()->sameValueAs($url->getFragmentIdentifier());
}
/**
* Returns the domain of the Url.
*
* @return Hostname|IPAddress
*/
public function getDomain(): Domain
{
return clone $this->domain;
}
/**
* Returns the fragment identifier of the Url.
*
* @return FragmentIdentifier
*/
public function getFragmentIdentifier(): FragmentIdentifier
{
return clone $this->fragmentIdentifier;
}
/**
* Returns the password part of the Url.
*
* @return StringLiteral
*/
public function getPassword(): StringLiteral
{
return clone $this->password;
}
/**
* Returns the path of the Url.
*
* @return Path
*/
public function getPath(): Path
{
return clone $this->path;
}
/**
* Returns the port of the Url.
*
* @return PortNumberInterface
*/
public function getPort(): PortNumberInterface
{
return clone $this->port;
}
/**
* Returns the query string of the Url.
*
* @return QueryString
*/
public function getQueryString(): QueryString
{
return clone $this->queryString;
}
/**
* Returns the scheme of the Url.
*
* @return SchemeName
*/
public function getScheme(): SchemeName
{
return clone $this->scheme;
}
/**
* Returns the user part of the Url.
*
* @return StringLiteral
*/
public function getUser(): StringLiteral
{
return clone $this->user;
}
}