UrlBuilder::setCredentials()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.2
cc 4
eloc 11
nc 4
nop 4
1
<?php
2
3
namespace Weew\Url;
4
5
class UrlBuilder implements IUrlBuilder {
6
    /**
7
     * @var string
8
     */
9
    protected $pattern = ':scheme:credentials:host:port:path:query:fragment';
10
11
    /**
12
     * @param IUrl $url
13
     * @param bool $encode
14
     *
15
     * @return string
16
     */
17
    public function build(IUrl $url, $encode = false) {
18
        $pattern = $this->pattern;
19
        $pattern = $this->setProtocol($pattern, $url, $encode);
20
        $pattern = $this->setCredentials($pattern, $url, $encode);
21
        $pattern = $this->setHost($pattern, $url, $encode);
22
        $pattern = $this->setPort($pattern, $url, $encode);
23
        $pattern = $this->setPath($pattern, $url, $encode);
24
        $pattern = $this->setQuery($pattern, $url, $encode);
25
        $pattern = $this->setFragment($pattern, $url, $encode);
26
27
        return $pattern;
28
    }
29
30
    /**
31
     * @param string $tld
32
     * @param string $domain
33
     * @param string $subdomain
34
     *
35
     * @return string
36
     */
37
    public function buildHost($tld, $domain, $subdomain) {
38
        $parts = [];
39
40
        if ($tld !== null) {
41
            array_unshift($parts, $tld);
42
        }
43
44
        if ($domain !== null) {
45
            array_unshift($parts, $domain);
46
        }
47
48
        if ($subdomain !== null) {
49
            array_unshift($parts, $subdomain);
50
        }
51
52
        $host = implode('.', $parts);
53
54
        return $host;
55
    }
56
57
    /**
58
     * @param string $pattern
59
     * @param IUrl $url
60
     * @param bool $encode
61
     * @param string $key
62
     *
63
     * @return string
64
     */
65
    protected function setProtocol($pattern, IUrl $url, $encode = false, $key = ':scheme') {
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
        $scheme = $url->getProtocol();
67
68
        if ($scheme) {
69
            $scheme = s('%s://', $scheme);
70
        }
71
72
        $pattern = s($pattern, [$key => $scheme]);
73
74
        return $pattern;
75
    }
76
77
    /**
78
     * @param string $pattern
79
     * @param IUrl $url
80
     * @param bool $encode
81
     * @param string $key
82
     *
83
     * @return string
84
     */
85
    protected function setCredentials($pattern, IUrl $url, $encode = false, $key = ':credentials') {
86
        $username = $url->getUsername();
87
        $password = $url->getPassword();
88
        $credentials = '';
89
90
        if ($encode) {
91
            $username = rawurlencode($username);
92
            $password = rawurlencode($password);
93
        }
94
95
        if ($username && $password) {
96
            $credentials = s('%s:%s@', $username, $password);
97
        }
98
99
        $pattern = s($pattern, [$key => $credentials]);
100
101
        return $pattern;
102
    }
103
104
    /**
105
     * @param string $pattern
106
     * @param IUrl $url
107
     * @param bool $encode
108
     * @param string $key
109
     *
110
     * @return string
111
     */
112
    protected function setHost($pattern, IUrl $url, $encode = false, $key = ':host') {
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
        $host = $url->getHost();
114
        $pattern = s($pattern, [$key => $host]);
115
116
        return $pattern;
117
    }
118
119
    /**
120
     * @param string $pattern
121
     * @param IUrl $url
122
     * @param bool $encode
123
     * @param string $key
124
     *
125
     * @return string
126
     */
127
    protected function setPort($pattern, IUrl $url, $encode = false, $key = ':port') {
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
        $port = $url->getPort();
129
130
        if ($port) {
131
            $port = s(':%s', $port);
132
        }
133
134
        $pattern = s($pattern, [$key => $port]);
135
136
        return $pattern;
137
    }
138
139
    /**
140
     * @param string $pattern
141
     * @param IUrl $url
142
     * @param bool $encode
143
     * @param string $key
144
     *
145
     * @return string
146
     */
147
    protected function setPath($pattern, IUrl $url, $encode = false, $key = ':path') {
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
        $path = $url->getPath();
149
        $pattern = s($pattern, [$key => $path]);
150
151
        return $pattern;
152
    }
153
154
    /**
155
     * @param string $pattern
156
     * @param IUrl $url
157
     * @param bool $encode
158
     * @param string $key
159
     *
160
     * @return string
161
     */
162
    protected function setQuery($pattern, IUrl $url, $encode = false, $key = ':query') {
163
        $query = $url->getQuery()->toString($encode);
164
165
        if ($query) {
166
            $query = s('?%s', $query);
167
        }
168
169
        $pattern = s($pattern, [$key => $query]);
170
171
        return $pattern;
172
    }
173
174
    /**
175
     * @param string $pattern
176
     * @param IUrl $url
177
     * @param bool $encode
178
     * @param string $key
179
     *
180
     * @return string
181
     */
182
    protected function setFragment($pattern, IUrl $url, $encode = false, $key = ':fragment') {
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
        $fragment = $url->getFragment();
184
185
        if ($fragment) {
186
            $fragment = s('#%s', $fragment);
187
        }
188
189
        $pattern = s($pattern, [$key => $fragment]);
190
191
        return $pattern;
192
    }
193
}
194