Passed
Push — master ( 2f3c29...61156f )
by Ben
02:48
created

Url::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Url;
4
5
class Url
6
{
7
    /** @var ParsedUrl */
8
    private $parsedUrl;
9
10 19
    private function __construct(ParsedUrl $parsedUrl)
11
    {
12 19
        $this->parsedUrl = $parsedUrl;
13 19
    }
14
15 20
    public static function fromString(string $url)
16
    {
17 20
        return new static( ParsedUrl::fromString($url) );
18
    }
19
20 4
    public function setCustomRoot(Root $root)
21
    {
22 4
        $this->parsedUrl = $this->parsedUrl->replaceRoot($root);
23
24 4
        return $this;
25
    }
26
27 5
    public function secure()
28
    {
29 5
        return $this->scheme(true);
30
    }
31
32 3
    public function nonSecure()
33
    {
34 3
        return $this->scheme(false);
35
    }
36
37 7
    private function scheme(bool $secure = true)
38
    {
39 7
        $this->parsedUrl = $this->parsedUrl->replaceScheme($secure ? 'https' : 'http');
40
41 7
        return $this;
42
    }
43
44 16
    public function get()
45
    {
46 16
        return $this->parsedUrl->get();
47
    }
48
49 1
    public function getScheme(): ?string
50
    {
51 1
        return $this->parsedUrl->scheme();
52
    }
53
54 1
    public function getHost(): ?string
55
    {
56 1
        return $this->parsedUrl->host();
57
    }
58
59 1
    public function getPort(): ?string
60
    {
61 1
        return $this->parsedUrl->port();
62
    }
63
64 1
    public function getPath(): ?string
65
    {
66 1
        return $this->parsedUrl->path();
67
    }
68
69 1
    public function getQuery(): ?string
70
    {
71 1
        return $this->parsedUrl->query();
72
    }
73
74 1
    public function getHash(): ?string
75
    {
76 1
        return $this->parsedUrl->hash();
77
    }
78
79 1
    public function hasScheme(): bool
80
    {
81 1
        return $this->parsedUrl->hasScheme();
82
    }
83
84 1
    public function hasHost(): bool
85
    {
86 1
        return $this->parsedUrl->hasHost();
87
    }
88
89 1
    public function hasPort(): bool
90
    {
91 1
        return $this->parsedUrl->hasPort();
92
    }
93
94 1
    public function hasPath(): bool
95
    {
96 1
        return $this->parsedUrl->hasPath();
97
    }
98
99 1
    public function hasQuery(): bool
100
    {
101 1
        return $this->parsedUrl->hasQuery();
102
    }
103
104 1
    public function hasHash(): bool
105
    {
106 1
        return $this->parsedUrl->hasHash();
107
    }
108
109 1
    public function isAbsolute(): bool
110
    {
111 1
        return $this->parsedUrl->hasHost();
112
    }
113
114 9
    public function localize(string $localeSegment = null, array $available_locales = [])
115
    {
116 9
        $localizedPath = str_replace('//', '/',
117 9
            rtrim('/'.trim($localeSegment.$this->delocalizePath($available_locales), '/'), '/')
118
        );
119
120 9
        $this->parsedUrl = $this->parsedUrl->replacePath($localizedPath);
121
122 9
        return $this;
123
    }
124
125 9
    private function delocalizePath(array $available_locales)
126
    {
127 9
        if (!$this->parsedUrl->hasPath()) {
128 3
            return;
129
        }
130
131 9
        $path_segments = explode('/', trim($this->parsedUrl->path(), '/'));
132
133
        // Remove the locale segment if present
134 9
        if (in_array($path_segments[0], array_keys($available_locales))) {
135 1
            unset($path_segments[0]);
136
        }
137
138 9
        return '/'.implode('/', $path_segments);
139
    }
140
141 1
    public function __toString(): string
142
    {
143 1
        return $this->get();
144
    }
145
}
146