1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Url |
13
|
|
|
* Immutable Value Object representing a URL |
14
|
|
|
* But just a really simple representation |
15
|
|
|
* ie: does not fully support FTP or authority (username, password, port) |
16
|
|
|
* |
17
|
|
|
* @package EventEspresso\core\domain\values |
18
|
|
|
* @author Brent Christensen |
19
|
|
|
* @since 4.9.54 |
20
|
|
|
*/ |
21
|
|
|
class Url |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string $scheme |
26
|
|
|
*/ |
27
|
|
|
private $scheme; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string $host |
31
|
|
|
*/ |
32
|
|
|
private $host; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string $path |
36
|
|
|
*/ |
37
|
|
|
private $path; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string $query |
41
|
|
|
*/ |
42
|
|
|
private $query; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string $fragment |
46
|
|
|
*/ |
47
|
|
|
private $fragment; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Url constructor. |
52
|
|
|
* |
53
|
|
|
* @param $url |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
public function __construct($url) |
57
|
|
|
{ |
58
|
|
|
if ( |
59
|
|
|
! filter_var( |
60
|
|
|
$url, |
61
|
|
|
FILTER_VALIDATE_URL, |
62
|
|
|
array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED) |
63
|
|
|
) |
64
|
|
|
) { |
65
|
|
|
throw new InvalidArgumentException(esc_html__('Invalid URL. Both the "Scheme" and "Host" are required.', |
66
|
|
|
'event_espresso')); |
67
|
|
|
} |
68
|
|
|
$url = parse_url($url); |
69
|
|
|
$this->setScheme($url); |
70
|
|
|
$this->setHost($url); |
71
|
|
|
$this->setPath($url); |
72
|
|
|
$this->setQuery($url); |
73
|
|
|
$this->setFragment($url); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
79
|
|
|
* will return a string like: 'abc://' |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function scheme() |
84
|
|
|
{ |
85
|
|
|
return $this->scheme; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $url |
91
|
|
|
*/ |
92
|
|
|
private function setScheme($url) |
93
|
|
|
{ |
94
|
|
|
$this->scheme = $url['scheme'] . '://'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
100
|
|
|
* will return a string like: 'example.com' |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function host() |
105
|
|
|
{ |
106
|
|
|
return $this->host; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $url |
112
|
|
|
*/ |
113
|
|
|
private function setHost($url) |
114
|
|
|
{ |
115
|
|
|
$this->host = $url['host']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
121
|
|
|
* will return a string like: '/path/data' |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function path() |
126
|
|
|
{ |
127
|
|
|
return $this->path; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $url |
133
|
|
|
*/ |
134
|
|
|
private function setPath($url) |
135
|
|
|
{ |
136
|
|
|
$this->path = isset($url['path']) ? $url['path'] : ''; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
142
|
|
|
* will return a string like: '?key=value' |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function queryString() |
147
|
|
|
{ |
148
|
|
|
return $this->query !== '' ? '?' . $this->query : ''; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
154
|
|
|
* will return an array like: array('key' => 'value') |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function queryParams() |
159
|
|
|
{ |
160
|
|
|
return wp_parse_args($this->query); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $url |
166
|
|
|
*/ |
167
|
|
|
private function setQuery($url) |
168
|
|
|
{ |
169
|
|
|
$this->query = isset($url['query']) ? $url['query'] : ''; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
175
|
|
|
* will return a string like: '#id' |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function fragment() |
180
|
|
|
{ |
181
|
|
|
return $this->fragment !== '' ? '#' . $this->fragment : ''; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param array $url |
187
|
|
|
*/ |
188
|
|
|
private function setFragment($url) |
189
|
|
|
{ |
190
|
|
|
$this->fragment = isset($url['fragment']) ? $url['fragment'] : ''; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
196
|
|
|
* will return a string like: 'abc://example.com/path/data?key=value#id' |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getFullUrl() |
201
|
|
|
{ |
202
|
|
|
return $this->scheme() . $this->host() . $this->path() . $this->queryString() . $this->fragment(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
208
|
|
|
* will return a string like: 'abc://example.com/path/data?key=value#id' |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function __toString() |
213
|
|
|
{ |
214
|
|
|
return $this->getFullUrl(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|