|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Wrapper for League\Uri. |
|
5
|
|
|
* |
|
6
|
|
|
* Permits to cache registrableDomain and Origin |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace PiedWeb\UrlHarvester; |
|
10
|
|
|
|
|
11
|
|
|
use League\Uri\Http; |
|
12
|
|
|
use League\Uri\UriInfo; |
|
13
|
|
|
use League\Uri\UriResolver; |
|
14
|
|
|
|
|
15
|
|
|
class Url |
|
16
|
|
|
{ |
|
17
|
|
|
protected $http; |
|
18
|
|
|
protected $origin; |
|
19
|
|
|
protected $registrableDomain; |
|
20
|
|
|
|
|
21
|
45 |
|
public function __construct(string $url) |
|
22
|
|
|
{ |
|
23
|
45 |
|
$this->http = Http::createFromString($url); |
|
24
|
|
|
|
|
25
|
45 |
|
if (!UriInfo::isAbsolute($this->http)) { |
|
26
|
|
|
throw new \Exception('$url must be absolute (`'.$url.'`)'); |
|
27
|
|
|
} |
|
28
|
45 |
|
} |
|
29
|
|
|
|
|
30
|
21 |
|
public function resolve($url): string |
|
31
|
|
|
{ |
|
32
|
21 |
|
$resolved = UriResolver::resolve(Http::createFromString($url), $this->http); |
|
33
|
|
|
|
|
34
|
21 |
|
return $resolved->__toString(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getHttp() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->http; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getScheme() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->http->getScheme(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getHost() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->http->getHost(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
21 |
|
public function getOrigin() |
|
53
|
|
|
{ |
|
54
|
21 |
|
$this->origin = $this->origin ?? $this->origin = UriInfo::getOrigin($this->http); |
|
55
|
|
|
|
|
56
|
21 |
|
return $this->origin; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
public function getRegistrableDomain() |
|
60
|
|
|
{ |
|
61
|
12 |
|
return $this->registrableDomain |
|
62
|
12 |
|
?? ($this->registrableDomain = Domain::getRegistrableDomain($this->http->getHost())); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
9 |
|
public function getDocumentUrl(): string |
|
66
|
|
|
{ |
|
67
|
9 |
|
return $this->http->withFragment(''); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
15 |
|
public function get() |
|
71
|
|
|
{ |
|
72
|
15 |
|
return $this->__toString(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
18 |
|
public function __toString() |
|
76
|
|
|
{ |
|
77
|
18 |
|
return (string) $this->http; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function relativize() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return substr($this->get(), strlen($this->getOrigin())); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|