Passed
Push — master ( 9abeb8...36c770 )
by Dev
13:09
created

Url::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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
    public function __construct(string $url)
22
    {
23
        $this->http = Http::createFromString($url);
24
25
        if (!UriInfo::isAbsolute($this->http)) {
26
            throw new \Exception('$url must be absolute (`'.$url.'`)');
27
        }
28
    }
29
30
    public function resolve($url): string
31
    {
32
        $resolved = UriResolver::resolve(Http::createFromString($url), $this->http);
33
34
        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
    public function getOrigin()
53
    {
54
        $this->origin = $this->origin ?? $this->origin = UriInfo::getOrigin($this->http);
55
56
        return $this->origin;
57
    }
58
59
    public function getRegistrableDomain()
60
    {
61
        return $this->registrableDomain
62
            ?? ($this->registrableDomain = Domain::getRegistrableDomain($this->http->getHost()));
63
    }
64
65
    public function getDocumentUrl(): string
66
    {
67
        return $this->http->withFragment('');
68
    }
69
70
    public function get()
71
    {
72
        return $this->__toString();
73
    }
74
75
    public function __toString()
76
    {
77
        return (string) $this->http;
78
    }
79
}
80