Passed
Push — master ( 8fd177...8c2cea )
by Rougin
03:08
created

Uri::instance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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