1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GFG\DTOUrl; |
4
|
|
|
|
5
|
|
|
use GFG\DTOContext\DataWrapper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateField) |
9
|
|
|
* @method string getScheme() |
10
|
|
|
* @method string getHost() |
11
|
|
|
* @method string getPort() |
12
|
|
|
* @method string getUser() |
13
|
|
|
* @method string getPass() |
14
|
|
|
* @method array getPath() |
15
|
|
|
* @method array getQuery() |
16
|
|
|
* @method string getFragment() |
17
|
|
|
* @method Url setScheme(string $scheme) |
18
|
|
|
* @method Url setHost(string $host) |
19
|
|
|
* @method Url setPort(string $port) |
20
|
|
|
* @method Url setUser(string $user) |
21
|
|
|
* @method Url setPass(string $pass) |
22
|
|
|
* @method Url setPath(array $path) |
23
|
|
|
* @method Url setQuery(array $query) |
24
|
|
|
* @method Url setFragment(string $fragment) |
25
|
|
|
*/ |
26
|
|
|
class Url extends Datawrapper\Base |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* All the parts from the url are stored separetely even after treated, |
30
|
|
|
* this will allow to change specific bits without the need to rebuild the |
31
|
|
|
* entire url |
32
|
|
|
* |
33
|
|
|
* @var associative array |
34
|
|
|
*/ |
35
|
|
|
private $fullUrl = [ |
36
|
|
|
'scheme' => '', |
37
|
|
|
'credentials' => '', |
38
|
|
|
'host' => '', |
39
|
|
|
'port' => '', |
40
|
|
|
'path' => '', |
41
|
|
|
'query' => '', |
42
|
|
|
'fragment' => '' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $scheme; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $host; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $port; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $user; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $pass; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private $path = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Associative array to build query string |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
private $query = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $fragment; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build the final url based on the structure defined |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
5 |
|
public function getFullUrl() |
93
|
|
|
{ |
94
|
5 |
|
$parts = $this->toArray(); |
95
|
5 |
|
unset($parts['full_url'], $parts['pass']); |
96
|
|
|
|
97
|
5 |
|
foreach ($parts as $method => $param) { |
98
|
5 |
|
if ($param) { |
99
|
5 |
|
$method = 'add' . ucfirst($method); |
100
|
5 |
|
$this->$method($param); |
101
|
5 |
|
} |
102
|
5 |
|
} |
103
|
|
|
|
104
|
5 |
|
return ltrim(implode($this->fullUrl), '/'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add host to the final url |
109
|
|
|
* |
110
|
|
|
* @param string $host |
111
|
|
|
* @return Url |
112
|
|
|
*/ |
113
|
4 |
|
public function addHost($host) |
114
|
|
|
{ |
115
|
4 |
|
$this->fullUrl['host'] = $host; |
116
|
4 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add port to the final url |
121
|
|
|
* |
122
|
|
|
* @param string $port |
123
|
|
|
* @return Url |
124
|
|
|
*/ |
125
|
3 |
|
public function addPort($port) |
126
|
|
|
{ |
127
|
3 |
|
$this->fullUrl['port'] = ":{$port}"; |
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add scheme to the final url |
133
|
|
|
* |
134
|
|
|
* @param string $scheme |
135
|
|
|
* @return Url |
136
|
|
|
*/ |
137
|
4 |
|
public function addScheme($scheme) |
138
|
|
|
{ |
139
|
4 |
|
$this->fullUrl['scheme'] = $scheme . '://'; |
140
|
4 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add the credentials to the final url, both username and password are |
145
|
|
|
* added in this stage |
146
|
|
|
* |
147
|
|
|
* @param string $user |
148
|
|
|
* @return Url |
149
|
|
|
*/ |
150
|
3 |
|
public function addUser($user) |
151
|
|
|
{ |
152
|
|
|
//only add the information if there are username and password |
153
|
3 |
|
if ($user && $this->getPass()) { |
154
|
3 |
|
$this->fullUrl['credentials'] = $user . ':' . $this->getPass() . '@'; |
155
|
3 |
|
} |
156
|
|
|
|
157
|
3 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Add paths to the final url |
162
|
|
|
* |
163
|
|
|
* @param string $path |
164
|
|
|
* @return Url |
165
|
|
|
*/ |
166
|
5 |
|
public function addPath($path) |
167
|
|
|
{ |
168
|
5 |
|
$this->fullUrl['path'] = '/' . implode('/', (array) $path); |
169
|
5 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add query string to the final url |
174
|
|
|
* |
175
|
|
|
* @param array $query |
176
|
|
|
* @return Url |
177
|
|
|
*/ |
178
|
3 |
|
public function addQuery(array $query) |
179
|
|
|
{ |
180
|
3 |
|
$queryParts = []; |
181
|
3 |
|
foreach ($query as $key => $value) { |
182
|
3 |
|
$queryParts[] = "{$key}={$value}"; |
183
|
3 |
|
} |
184
|
|
|
|
185
|
3 |
|
$this->fullUrl['query'] = '?' . implode('&', $queryParts); |
186
|
|
|
|
187
|
3 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add fragment to the final url |
192
|
|
|
* |
193
|
|
|
* @param string $fragment |
194
|
|
|
* @return Url |
195
|
|
|
*/ |
196
|
3 |
|
public function addFragment($fragment) |
197
|
|
|
{ |
198
|
3 |
|
$this->fullUrl['fragment'] = "#{$fragment}"; |
199
|
3 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Replace path values |
204
|
|
|
* |
205
|
|
|
* @param associative array $replace |
206
|
|
|
* @return Url |
207
|
|
|
*/ |
208
|
2 |
|
public function replacePath(array $replace) |
209
|
|
|
{ |
210
|
2 |
|
return $this->setPath(array_merge($this->getPath(), $replace)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Replace query string values |
215
|
|
|
* |
216
|
|
|
* @param associative array $replace |
217
|
|
|
* @return Url |
218
|
|
|
*/ |
219
|
1 |
|
public function replaceQuery(array $replace) |
220
|
|
|
{ |
221
|
1 |
|
return $this->setQuery(array_merge($this->getQuery(), $replace)); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|