Uri::hasPort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace CodeJet\Http;
4
5
use Psr\Http\Message\UriInterface;
6
use InvalidArgumentException;
7
8
class Uri implements UriInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $scheme = '';
14
15
    /**
16
     * @var string
17
     */
18
    private $userInfo = '';
19
20
    /**
21
     * @var string
22
     */
23
    private $host = '';
24
25
    /**
26
     * @var int
27
     */
28
    private $port;
29
30
    /**
31
     * @var string
32
     */
33
    private $path = '';
34
35
    /**
36
     * @var string
37
     */
38
    private $query = '';
39
40
    /**
41
     * @var string
42
     */
43
    private $fragment = '';
44
45
    /**
46
     * @var int[] Default Ports for known Schemes.
47
     */
48
    private $defaultSchemePorts = [
49
        'http' => 80,
50
        'https' => 443,
51
    ];
52
53
    /**
54
     * @param string|array $uri
55
     * @throws InvalidArgumentException on non-string $uri argument
56
     */
57 108
    public function __construct($uri = '')
58
    {
59 108
        if (is_array($uri)) {
60 45
            $this->parseUriParts($uri);
61
        }
62
63 108
        if (is_string($uri) && !empty($uri)) {
64 81
            $parts = parse_url($uri);
65 81
            if (false === $parts) {
66 6
                throw new \InvalidArgumentException(
67 6
                    'The source URI string appears to be malformed'
68
                );
69
            }
70
71 75
            $this->parseUriParts($parts);
72
        }
73 102
    }
74
75 51
    public function __toString()
76
    {
77 51
        return $this->getUriString();
78
    }
79
80 75
    private function getUriString()
81
    {
82 75
        $uri = '';
83
84 75
        if ($this->hasScheme()) {
85 51
            $uri .= sprintf('%s://', $this->getScheme());
86
        }
87
88 75
        $uri .= $this->getAuthority() . $this->getPath();
89
90 75
        if ($this->hasQuery()) {
91 24
            $uri .= sprintf('?%s', $this->getQuery());
92
        }
93
94 75
        if ($this->hasFragment()) {
95 24
            $uri .= sprintf('#%s', $this->getFragment());
96
        }
97
98 75
        return $uri;
99
    }
100
101 99
    private function parseUriParts($parts)
102
    {
103 99
        $this->scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
104 99
        $this->userInfo = isset($parts['user']) ? $parts['user'] : '';
105 99
        $this->host = isset($parts['host']) ? $parts['host'] : '';
106 99
        $this->port = isset($parts['port']) ? $parts['port'] : null;
107 99
        $this->path = isset($parts['path']) ? $parts['path'] : '';
108 99
        $this->query = isset($parts['query']) ? $parts['query'] : '';
109 99
        $this->fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
110
111 99
        if (isset($parts['pass'])) {
112 30
            $this->userInfo .= ':' . $parts['pass'];
113
        }
114 99
    }
115
116 87
    public function hasScheme()
117
    {
118 87
        if (!empty($this->scheme)) {
119 66
            return true;
120
        }
121
122 24
        return false;
123
    }
124
125 66
    public function getScheme()
126
    {
127 66
        return strtolower($this->scheme);
128
    }
129
130 81
    public function getAuthority()
131
    {
132 81
        if (!$this->hasHost()) {
133 24
            return '';
134
        }
135
136 57
        $authority = $this->getHost();
137
138 57
        if ($this->hasUserInfo()) {
139 24
            $authority = $this->getUserInfo() . '@' . $authority;
140
        }
141
142 57
        if (!$this->isDefaultPort()) {
143 24
            $authority .= ':' . $this->getPort();
144
        }
145
146 57
        return $authority;
147
    }
148
149 27
    public function getUserInfo()
150
    {
151 27
        return $this->userInfo;
152
    }
153
154 63
    public function hasUserInfo()
155
    {
156 63
        if (!empty($this->userInfo)) {
157 27
            return true;
158
        }
159
160 36
        return false;
161
    }
162
163 84
    public function hasHost()
164
    {
165 84
        if (!empty($this->host)) {
166 63
            return true;
167
        }
168
169 24
        return false;
170
    }
171
172 75
    public function getHost()
173
    {
174 75
        return strtolower($this->host);
175
    }
176
177 66
    public function hasPort()
178
    {
179 66
        if (!empty($this->port)) {
180 39
            return true;
181
        }
182
183 30
        return false;
184
    }
185
186 30
    public function getPort()
187
    {
188 30
        if ($this->isDefaultPort()) {
189 3
            return null;
190
        }
191
192 27
        return $this->port;
193
    }
194
195 63
    private function isDefaultPort()
196
    {
197 63
        if (!$this->hasPort()) {
198 27
            return true;
199
        }
200
201 36
        if (!$this->hasScheme()) {
202 3
            return false;
203
        }
204
205 33
        if (isset($this->defaultSchemePorts[$this->getScheme()])
206 33
            && $this->defaultSchemePorts[$this->getScheme()] == $this->port
207
        ) {
208 9
            return true;
209
        }
210
211 24
        return false;
212
    }
213
214 9
    public function hasPath()
215
    {
216 9
        if (!empty($this->path)) {
217 6
            return true;
218
        }
219
220 3
        return false;
221
    }
222
223 78
    public function getPath()
224
    {
225
226 78
        return $this->path;
227
    }
228
229 78
    public function hasQuery()
230
    {
231 78
        if (!empty($this->query)) {
232 27
            return true;
233
        }
234
235 51
        return false;
236
    }
237
238 30
    public function getQuery()
239
    {
240 30
        return $this->query;
241
    }
242
243 78
    public function hasFragment()
244
    {
245 78
        if (!empty($this->fragment)) {
246 27
            return true;
247
        }
248
249 51
        return false;
250
    }
251
252 27
    public function getFragment()
253
    {
254 27
        return $this->fragment;
255
    }
256
257 9
    public function withScheme($scheme)
258
    {
259 9
        return $this->withModifiedParts(
260 9
            ['scheme' => $scheme]
261
        );
262
    }
263
264 6
    public function withUserInfo($user, $password = null)
265
    {
266 6
        return $this->withModifiedParts(
267
            [
268 6
                'user' => $user,
269 6
                'pass' => $password
270
            ]
271
        );
272
    }
273
274 6
    public function withHost($host)
275
    {
276 6
        return $this->withModifiedParts(
277 6
            ['host' => $host]
278
        );
279
    }
280
281 6
    public function withPort($port)
282
    {
283 6
        return $this->withModifiedParts(
284 6
            ['port' => $port]
285
        );
286
    }
287
288 6
    public function withPath($path)
289
    {
290 6
        return $this->withModifiedParts(
291 6
            ['path' => $path]
292
        );
293
    }
294
295 6
    public function withQuery($query)
296
    {
297 6
        return $this->withModifiedParts(
298 6
            ['query' => $query]
299
        );
300
    }
301
302 6
    public function withFragment($fragment)
303
    {
304 6
        return $this->withModifiedParts(
305 6
            ['fragment' => $fragment]
306
        );
307
    }
308
309
    /**
310
     * @param array $modifiedParts
311
     * @return Uri
312
     */
313 45
    private function withModifiedParts($modifiedParts)
314
    {
315 45
        $currentParts = parse_url($this->getUriString());
316
317 45
        $newUriParts = array_merge($currentParts, $modifiedParts);
318
319 45
        return new self($newUriParts);
320
    }
321
}
322