Passed
Push — master ( e9906c...54b727 )
by Zlatin
01:25
created

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