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 | |||||
19 | protected $origin; |
||||
20 | |||||
21 | 48 | protected $registrableDomain; |
|||
22 | |||||
23 | 48 | public function __construct(string $url) |
|||
24 | { |
||||
25 | 48 | $this->http = Http::createFromString($url); |
|||
26 | |||||
27 | if (! UriInfo::isAbsolute($this->http)) { |
||||
28 | 48 | throw new \Exception('$url must be absolute (`'.$url.'`)'); |
|||
29 | } |
||||
30 | 21 | } |
|||
31 | |||||
32 | 21 | public function resolve($url): string |
|||
33 | { |
||||
34 | 21 | $resolved = UriResolver::resolve(Http::createFromString($url), $this->http); |
|||
35 | |||||
36 | return $resolved->__toString(); |
||||
37 | } |
||||
38 | |||||
39 | public function getHttp() |
||||
40 | { |
||||
41 | return $this->http; |
||||
42 | } |
||||
43 | |||||
44 | public function getScheme() |
||||
45 | { |
||||
46 | return $this->http->getScheme(); |
||||
47 | } |
||||
48 | |||||
49 | public function getHost() |
||||
50 | { |
||||
51 | return $this->http->getHost(); |
||||
52 | 24 | } |
|||
53 | |||||
54 | 24 | public function getOrigin() |
|||
55 | { |
||||
56 | 24 | $this->origin = $this->origin ?? $this->origin = UriInfo::getOrigin($this->http); |
|||
57 | |||||
58 | return $this->origin; |
||||
59 | 12 | } |
|||
60 | |||||
61 | 12 | public function getRegistrableDomain() |
|||
62 | 12 | { |
|||
63 | return $this->registrableDomain |
||||
64 | ?? ($this->registrableDomain = Domain::getRegistrableDomain($this->http->getHost())); |
||||
65 | 9 | } |
|||
66 | |||||
67 | 9 | public function getDocumentUrl(): string |
|||
68 | { |
||||
69 | return $this->http->withFragment(''); |
||||
70 | 3 | } |
|||
71 | |||||
72 | 3 | public function getRelativizedDocumentUrl(): string |
|||
73 | { |
||||
74 | return substr($this->http->withFragment(''), \strlen($this->getOrigin())); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
75 | 15 | } |
|||
76 | |||||
77 | 15 | public function get(): string |
|||
78 | { |
||||
79 | return $this->__toString(); |
||||
80 | 18 | } |
|||
81 | |||||
82 | 18 | public function __toString(): string |
|||
83 | { |
||||
84 | return (string) $this->http; |
||||
85 | 3 | } |
|||
86 | |||||
87 | 3 | public function relativize() |
|||
88 | { |
||||
89 | return substr($this->get(), \strlen($this->getOrigin())); |
||||
0 ignored issues
–
show
It seems like
$this->getOrigin() can also be of type null ; however, parameter $string of strlen() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
90 | } |
||||
91 | } |
||||
92 |