scratch – Blame information for rev 126
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
126 | office | 1 | <?php |
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Uri |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Uri; |
||
12 | |||
13 | /** |
||
14 | * |
||
15 | * Manipulates and generates URLs. |
||
16 | * |
||
17 | * @package Aura.Uri |
||
18 | * |
||
19 | */ |
||
20 | class Url |
||
21 | { |
||
22 | /** |
||
23 | * Pattern for URL scheme matching |
||
24 | */ |
||
25 | const SCHEME_PATTERN = '#^(http|ftp)s?://#i'; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * The scheme (for example 'http' or 'https'). |
||
30 | * |
||
31 | * @var string |
||
32 | * |
||
33 | */ |
||
34 | protected $scheme; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * The username, if any. |
||
39 | * |
||
40 | * @var string |
||
41 | * |
||
42 | */ |
||
43 | protected $user; |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * The password, if any. |
||
48 | * |
||
49 | * @var string |
||
50 | * |
||
51 | */ |
||
52 | protected $pass; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * The Host object |
||
57 | * |
||
58 | * @var Host |
||
59 | * |
||
60 | */ |
||
61 | protected $host; |
||
62 | |||
63 | /** |
||
64 | * |
||
65 | * The port number (for example, '80'). |
||
66 | * |
||
67 | * @var string |
||
68 | * |
||
69 | */ |
||
70 | protected $port; |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * A Path object. |
||
75 | * |
||
76 | * @var Path |
||
77 | * |
||
78 | */ |
||
79 | protected $path; |
||
80 | |||
81 | /** |
||
82 | * |
||
83 | * A Query object. |
||
84 | * |
||
85 | * @var Query |
||
86 | * |
||
87 | */ |
||
88 | protected $query; |
||
89 | |||
90 | /** |
||
91 | * |
||
92 | * The fragment portion (for example, the "foo" in "#foo"). |
||
93 | * |
||
94 | * @var string |
||
95 | * |
||
96 | */ |
||
97 | protected $fragment; |
||
98 | |||
99 | // authority = userinfo@host:port |
||
100 | |||
101 | /** |
||
102 | * |
||
103 | * Constructor. |
||
104 | * |
||
105 | * @param string $scheme The URL scheme (e.g. `http`). |
||
106 | * |
||
107 | * @param string $user The username. |
||
108 | * |
||
109 | * @param string $pass The password. |
||
110 | * |
||
111 | * @param Host $host The host elements. |
||
112 | * |
||
113 | * @param int $port The port number. |
||
114 | * |
||
115 | * @param Path $path The path elements, including format. |
||
116 | * |
||
117 | * @param Query $query The query elements. |
||
118 | * |
||
119 | * @param string $fragment The fragment. |
||
120 | * |
||
121 | */ |
||
122 | public function __construct( |
||
123 | $scheme, |
||
124 | $user, |
||
125 | $pass, |
||
126 | Host $host, |
||
127 | $port, |
||
128 | Path $path, |
||
129 | Query $query, |
||
130 | $fragment |
||
131 | ) { |
||
132 | $this->scheme = $scheme; |
||
133 | $this->user = $user; |
||
134 | $this->pass = $pass; |
||
135 | $this->host = $host; |
||
136 | $this->port = $port; |
||
137 | $this->path = $path; |
||
138 | $this->query = $query; |
||
139 | $this->fragment = $fragment; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * |
||
144 | * Converts the URI object to a string and returns it. |
||
145 | * |
||
146 | * @return string The full URI this object represents. |
||
147 | * |
||
148 | */ |
||
149 | public function __toString() |
||
150 | { |
||
151 | return $this->getFull(true); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * |
||
156 | * Magic get for properties. |
||
157 | * |
||
158 | * @param string $key The property to get. |
||
159 | * |
||
160 | * @return mixed The value of the property. |
||
161 | * |
||
162 | */ |
||
163 | public function __get($key) |
||
164 | { |
||
165 | return $this->$key; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * |
||
170 | * Returns the URL as a string, not including scheme or host. |
||
171 | * |
||
172 | * @return string The URL string. |
||
173 | * |
||
174 | */ |
||
175 | public function get() |
||
176 | { |
||
177 | // get the query as a string |
||
178 | $query = $this->query->__toString(); |
||
179 | |||
180 | // we use trim() instead of empty() on string |
||
181 | // elements to allow for string-zero values. |
||
182 | return $this->path->__toString() |
||
183 | . (empty($query) ? '' : '?' . $query) |
||
184 | . (trim($this->fragment) === '' ? '' : '#' . rawurlencode($this->fragment)); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * |
||
189 | * Returns the URL as a string, including the scheme and host. |
||
190 | * |
||
191 | * @return string The URL string. |
||
192 | * |
||
193 | */ |
||
194 | public function getFull() |
||
195 | { |
||
196 | // start with the scheme |
||
197 | $url = empty($this->scheme) |
||
198 | ? '' |
||
199 | : rawurlencode($this->scheme) . '://'; |
||
200 | |||
201 | // add the username and password, if any. |
||
202 | if (! empty($this->user)) { |
||
203 | $url .= rawurlencode($this->user); |
||
204 | if (! empty($this->pass)) { |
||
205 | $url .= ':' . rawurlencode($this->pass); |
||
206 | } |
||
207 | $url .= '@'; |
||
208 | } |
||
209 | |||
210 | $host = $this->host->__toString(); |
||
211 | |||
212 | // add the host and port, if any. |
||
213 | $url .= (empty($this->host) ? '' : rawurlencode($this->host)) |
||
214 | . (empty($this->port) ? '' : ':' . (int) $this->port); |
||
215 | |||
216 | return $url . $this->get(); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * |
||
221 | * Returns the URL as a string, including the host but excluding the scheme |
||
222 | * |
||
223 | * @return string The URL string. |
||
224 | * |
||
225 | */ |
||
226 | public function getSchemeless() |
||
227 | { |
||
228 | return preg_replace(self::SCHEME_PATTERN, '//', $this->getFull(), 1); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * |
||
233 | * Set the scheme (for example 'http' or 'https'). |
||
234 | * |
||
235 | * @param string $scheme The scheme (for example 'http' or 'https'). |
||
236 | * |
||
237 | * @return $this |
||
238 | * |
||
239 | */ |
||
240 | public function setScheme($scheme) |
||
241 | { |
||
242 | $this->scheme = $scheme; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * |
||
249 | * Sets the username. |
||
250 | * |
||
251 | * @param string $user The username. |
||
252 | * |
||
253 | * @return $this |
||
254 | * |
||
255 | */ |
||
256 | public function setUser($user) |
||
257 | { |
||
258 | $this->user = $user; |
||
259 | |||
260 | return $this; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * |
||
265 | * Sets the password. |
||
266 | * |
||
267 | * @param string $pass The password. |
||
268 | * |
||
269 | * @return $this |
||
270 | * |
||
271 | */ |
||
272 | public function setPass($pass) |
||
273 | { |
||
274 | $this->pass = $pass; |
||
275 | |||
276 | return $this; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * |
||
281 | * Sets the Host object for this URL. |
||
282 | * |
||
283 | * @param Host $host The host name. |
||
284 | * |
||
285 | * @return $this |
||
286 | * |
||
287 | */ |
||
288 | public function setHost(Host $host) |
||
289 | { |
||
290 | $this->host = $host; |
||
291 | |||
292 | return $this; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * |
||
297 | * Sets the port number (for example, '80'). |
||
298 | * |
||
299 | * @param int $port The port number. |
||
300 | * |
||
301 | * @return $this |
||
302 | * |
||
303 | */ |
||
304 | public function setPort($port) |
||
305 | { |
||
306 | $this->port = $port; |
||
307 | |||
308 | return $this; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * |
||
313 | * Sets the Path object for this URL. |
||
314 | * |
||
315 | * @param Path $path The Path object. |
||
316 | * |
||
317 | * @return $this |
||
318 | * |
||
319 | */ |
||
320 | public function setPath(Path $path) |
||
321 | { |
||
322 | $this->path = $path; |
||
323 | |||
324 | return $this; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * |
||
329 | * Sets the Query object for this URL. |
||
330 | * |
||
331 | * @param Query $query The Query object. |
||
332 | * |
||
333 | * @return $this |
||
334 | * |
||
335 | */ |
||
336 | public function setQuery(Query $query) |
||
337 | { |
||
338 | $this->query = $query; |
||
339 | |||
340 | return $this; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * |
||
345 | * Sets the fragment portion (for example, the "foo" in "#foo"). |
||
346 | * |
||
347 | * @param string $fragment The fragment. |
||
348 | * |
||
349 | * @return $this |
||
350 | * |
||
351 | */ |
||
352 | public function setFragment($fragment) |
||
353 | { |
||
354 | $this->fragment = $fragment; |
||
355 | |||
356 | return $this; |
||
357 | } |
||
358 | } |