1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP Generics package. |
5
|
|
|
* |
6
|
|
|
* @package Generics |
7
|
|
|
*/ |
8
|
|
|
namespace Generics\Socket; |
9
|
|
|
|
10
|
|
|
use Generics\Util\UrlParser; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class provides a data holder for a url |
14
|
|
|
* |
15
|
|
|
* @author Maik Greubel <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Url extends Endpoint |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Scheme part of url |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $scheme; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Path part of url |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $path; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The query string |
36
|
|
|
*/ |
37
|
|
|
private $queryString; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new Url instance |
41
|
|
|
* |
42
|
|
|
* @param string $address |
43
|
|
|
* The address for the url (either only address or full url) |
44
|
|
|
* @param int $port |
45
|
|
|
* The port for the url |
46
|
|
|
* @param string $path |
47
|
|
|
* The path for the url |
48
|
|
|
* @param string $scheme |
49
|
|
|
* The scheme for the url |
50
|
|
|
*/ |
51
|
35 |
|
public function __construct($address, $port = 80, $path = '/', $scheme = 'http', $queryString = '') |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
35 |
|
$parsed = UrlParser::parseUrl($address); |
55
|
6 |
|
parent::__construct($parsed->getAddress(), $parsed->getPort()); |
56
|
6 |
|
$this->path = $parsed->getPath(); |
57
|
6 |
|
$this->scheme = $parsed->getScheme(); |
58
|
6 |
|
$this->queryString = $parsed->getQueryString(); |
59
|
35 |
|
} catch (InvalidUrlException $ex) { |
60
|
35 |
|
parent::__construct($address, $port); |
61
|
35 |
|
$this->path = $path; |
62
|
35 |
|
$this->scheme = $scheme; |
63
|
35 |
|
$this->queryString = $queryString; |
64
|
|
|
} |
65
|
35 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the scheme of the url |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
23 |
|
public function getScheme(): string |
73
|
|
|
{ |
74
|
23 |
|
return $this->scheme; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the path of the url |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
32 |
|
public function getPath(): string |
83
|
|
|
{ |
84
|
32 |
|
return $this->path; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retrieve the url as string |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
6 |
|
public function getUrlString(): string |
93
|
|
|
{ |
94
|
6 |
|
$query = ""; |
95
|
6 |
|
if (strlen($this->queryString) > 0) { |
96
|
1 |
|
$query = sprintf("?%s", $this->queryString); |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
if (($this->scheme == 'http' && $this->getPort() == 80) || ($this->scheme == 'ftp' && $this->getPort() == 21) || ($this->scheme == 'https' && $this->getPort() == 443)) { |
100
|
5 |
|
return sprintf("%s://%s%s%s", $this->scheme, $this->getAddress(), $this->path, $query); |
101
|
|
|
} |
102
|
1 |
|
return sprintf("%s://%s:%d%s%s", $this->scheme, $this->getAddress(), $this->getPort(), $this->path, $query); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve only the file part |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getFile(): string |
111
|
|
|
{ |
112
|
|
|
return basename($this->path); |
113
|
|
|
} |
114
|
|
|
|
115
|
31 |
|
public function getQueryString(): string |
116
|
|
|
{ |
117
|
31 |
|
return $this->queryString; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
* @see \Generics\Socket\Endpoint::__toString() |
124
|
|
|
*/ |
125
|
4 |
|
public function __toString(): string |
126
|
|
|
{ |
127
|
4 |
|
return $this->getUrlString(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|