|
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
|
|
|
* Create a new Url instance |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $address |
|
38
|
|
|
* The address for the url (either only address or full url) |
|
39
|
|
|
* @param int $port |
|
40
|
|
|
* The port for the url |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* The path for the url |
|
43
|
|
|
* @param string $scheme |
|
44
|
|
|
* The scheme for the url |
|
45
|
|
|
*/ |
|
46
|
17 |
|
public function __construct($address, $port = 80, $path = '/', $scheme = 'http') |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
17 |
|
$parsed = UrlParser::parseUrl($address); |
|
50
|
4 |
|
parent::__construct($parsed->getAddress(), $parsed->getPort()); |
|
51
|
4 |
|
$this->path = $parsed->getPath(); |
|
52
|
4 |
|
$this->scheme = $parsed->getScheme(); |
|
53
|
17 |
|
} catch (InvalidUrlException $ex) { |
|
54
|
17 |
|
parent::__construct($address, $port); |
|
55
|
17 |
|
$this->path = $path; |
|
56
|
17 |
|
$this->scheme = $scheme; |
|
57
|
|
|
} |
|
58
|
17 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the scheme of the url |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
14 |
|
public function getScheme() |
|
66
|
|
|
{ |
|
67
|
14 |
|
return $this->scheme; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the path of the url |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
14 |
|
public function getPath() |
|
76
|
|
|
{ |
|
77
|
14 |
|
return $this->path; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retrieve the url as string |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function getUrlString() |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (($this->scheme == 'http' && $this->getPort() == 80) || ($this->scheme == 'ftp' && $this->getPort() == 21) || ($this->scheme == 'https' && $this->getPort() == 443)) { |
|
88
|
3 |
|
return sprintf("%s://%s%s", $this->scheme, $this->getAddress(), $this->path); |
|
89
|
|
|
} |
|
90
|
1 |
|
return sprintf("%s://%s:%d%s", $this->scheme, $this->getAddress(), $this->getPort(), $this->path); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrieve only the file part |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getFile() |
|
99
|
|
|
{ |
|
100
|
|
|
return basename($this->path); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
* @see \Generics\Socket\Endpoint::__toString() |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function __toString() |
|
109
|
|
|
{ |
|
110
|
4 |
|
return $this->getUrlString(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|