This repository was archived by the owner on May 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUri.php
More file actions
202 lines (177 loc) · 3.68 KB
/
Uri.php
File metadata and controls
202 lines (177 loc) · 3.68 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
<?php
namespace http;
require_once 'Exception.php';
/**
* Parse urls: [scheme://][username[:password]@]host[:port]path
*/
class Uri {
const SCHEME_HTTP = 'http';
const SCHEME_HTTPS = 'https';
protected $scheme = self::SCHEME_HTTP;
protected $username = null;
protected $password = null;
protected $host;
protected $port = 80;
protected $path = '/';
protected $query = '';
protected $fragment = '';
/**
* @param string $url
*/
public function __construct($url) {
$parsedUrl = parse_url($url);
isset($parsedUrl['scheme']) && $this->setScheme($parsedUrl['scheme']);
isset($parsedUrl['user']) && $this->setUsername($parsedUrl['user']);
isset($parsedUrl['password']) && $this->setPassword($parsedUrl['password']);
isset($parsedUrl['host']) && $this->setHost($parsedUrl['host']);
isset($parsedUrl['port']) && $this->setPort($parsedUrl['port']);
isset($parsedUrl['path']) && $this->setPath($parsedUrl['path']);
isset($parsedUrl['query']) && $this->setQuery($parsedUrl['query']);
isset($parsedUrl['fragment']) && $this->setFragment($parsedUrl['fragment']);
}
/**
* @return string
*/
public function getScheme() {
return $this->scheme;
}
/**
* @param string $scheme
* @return Uri
* @throws \http\Exception
*/
public function setScheme($scheme) {
switch ($scheme) {
case self::SCHEME_HTTP:
// do nothing
break;
case self::SCHEME_HTTPS:
if ($this->getPort() === 80) {
$this->setPort(443);
}
break;
default:
throw new Exception('Unsupported uri scheme');
}
$this->scheme = $scheme;
return $this;
}
/**
* @return string|null
*/
public function getUsername() {
return $this->username;
}
/**
* @param string|null $username
* @return Uri
*/
public function setUsername($username) {
$this->username = $username === null? null: (string) $username;
return $this;
}
/**
* @return string|null
*/
public function getPassword() {
return $this->password;
}
/**
* @param string $password
* @return Uri
*/
public function setPassword($password) {
$this->password = $password === null? null: (string) $password;
return $this;
}
/**
* @return string
*/
public function getHost() {
return $this->host;
}
/**
* @param string $host
* @return Uri
*/
public function setHost($host) {
$this->host = (string) $host;
return $this;
}
/**
* @return int
*/
public function getPort() {
return $this->port;
}
/**
* @param int $port
* @return Uri
*/
public function setPort($port) {
$this->port = (int) $port;
return $this;
}
/**
* @return string
*/
public function getPath() {
return $this->path;
}
/**
* @param string $path
* @return Uri
*/
public function setPath($path) {
$this->path = '/' . ltrim($path, '/');
return $this;
}
/**
* @return string
*/
public function getQuery() {
return $this->query;
}
/**
* @param string $query
* @return Uri
*/
public function setQuery($query) {
$this->query = (string) $query;
return $this;
}
/**
* @return string
*/
public function getFragment() {
return $this->fragment;
}
/**
* @param string $fragment
* @return Uri
*/
public function setFragment($fragment) {
$this->fragment = ltrim($fragment, '#');
return $this;
}
/**
* @return string
*/
function __toString() {
$auth = $this->getUsername();
if ($password = $this->getPassword()) {
$auth .= ":{$password}";
}
if (!empty($auth)) {
$auth .= '@';
}
$uri = "{$this->getScheme()}://{$auth}{$this->getHost()}:{$this->getPort()}{$this->getPath()}";
if ($query = $this->getQuery()) {
$uri .= '?' . $query;
}
if ($fragment = $this->getFragment()) {
$uri .= '#' . $fragment;
}
return $uri;
}
}