Uri::createFormUrl()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 6.5222
c 0
b 0
f 0
cc 9
nc 256
nop 1
1
<?php
2
3
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq (Malik) <[email protected]>
7
 *
8
 * @link https://github.com/alphazframework/framework
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 * @since 1.0.0
13
 *
14
 * @license MIT
15
 */
16
17
namespace alphaz\http;
18
19
class Uri extends Message
20
{
21
    public function __construct()
22
    {
23
        parent::__construct();
24
    }
25
26
    /**
27
     * Set the Uri.
28
     *
29
     * @param (string) $url valid url
30
     *                      (string) $host valid host
31
     *                      (int) $port valid port number
32
     *                      (string) $path valid path
33
     *                      (string) $query query URI
34
     *                      (string) $fragment fragment
35
     *                      (string) $user username
36
     *                      (string) $password password
37
     *
38
     * @since 1.0.0
39
     *
40
     * @return object
41
     */
42
    public function setUri($scheme, $host, $port = null, $path = '/', $query = '', $fragment = '', $user = '', $password = '')
43
    {
44
        $this->scheme = $this->filterScheme($scheme);
45
        $this->host = $host;
46
        $this->port = $this->filterPort($port);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->filterPort($port) of type string is incompatible with the declared type integer|null of property $port.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
        $this->path = empty($path) ? '/' : $this->filterQuery($path);
48
        $this->query = $this->filterQuery($query);
49
        $this->fragment = $this->filterQuery($fragment);
50
        $this->user = $user;
51
        $this->password = $password;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Get the scheme component of the URL.
58
     *
59
     * @param (string) $url valid url
60
     *
61
     * @since 1.0.0
62
     *
63
     * @return object
64
     */
65
    public function createFormUrl($url)
66
    {
67
        $urlParts = parse_url($url);
68
69
        $scheme = isset($urlParts['scheme']) ? $urlParts['scheme'] : '';
70
        $user = isset($urlParts['user']) ? $urlParts['user'] : '';
71
        $password = isset($urlParts['pass']) ? $urlParts['pass'] : '';
72
        $host = isset($urlParts['host']) ? $urlParts['host'] : '';
73
        $port = isset($urlParts['port']) ? $urlParts['port'] : null;
74
        $path = isset($urlParts['path']) ? $urlParts['path'] : '';
75
        $query = isset($urlParts['query']) ? $urlParts['query'] : '';
76
        $fragment = isset($urlParts['fragment']) ? $urlParts['fragment'] : '';
77
78
        $this->setUri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Filter the url scheme component.
85
     *
86
     * @param (string) $scheme valid scheme
87
     *
88
     * @since 1.0.0
89
     *
90
     * @return string
91
     */
92
    public function filterScheme($scheme)
93
    {
94
        $valids = [
95
            ''      => true,
96
            'http'  => true,
97
            'https' => true,
98
        ];
99
        $scheme = str_replace('://', '', strtolower((string) $scheme));
100
        if (!isset($valids[$scheme])) {
101
            throw new \InvalidArgumentException('Uri scheme must be one of: "", "https", "http"');
102
        }
103
104
        return $scheme;
105
    }
106
107
    /**
108
     * Filter port.
109
     *
110
     * @param (int) $port port number
111
     *
112
     * @since 1.0.0
113
     *
114
     * @return string
115
     */
116
    public function filterPort($port)
117
    {
118
        if (is_null($port) || is_int($port)) {
0 ignored issues
show
introduced by
The condition is_int($port) is always true.
Loading history...
119
            return $port;
120
        }
121
122
        throw new \InvalidArgumentException('Uri port much be type int');
123
    }
124
125
    /**
126
     * Filter the Query uri.
127
     *
128
     * @param (string) $query query uri
129
     *
130
     * @since 1.0.0
131
     *
132
     * @return string
133
     */
134
    public function filterQuery($query)
135
    {
136
        return rawurlencode($query);
137
    }
138
139
    /**
140
     * Get the scheme component of the URL.
141
     *
142
     * @since 1.0.0
143
     *
144
     * @return string
145
     */
146
    public function getScheme()
147
    {
148
        return $this->scheme;
149
    }
150
151
    /**
152
     * Get determined scheme.
153
     *
154
     * @since 1.0.0
155
     *
156
     * @return string
157
     */
158
    public function getDeterminedScheme()
159
    {
160
        return ($this->isSecure()) ? 'https' : 'http';
0 ignored issues
show
introduced by
The method isSecure() does not exist on alphaz\http\Uri. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
        return ($this->/** @scrutinizer ignore-call */ isSecure()) ? 'https' : 'http';
Loading history...
161
    }
162
163
    /**
164
     * Get the authority component of the URL.
165
     *
166
     * @since 1.0.0
167
     *
168
     * @return string
169
     */
170
    public function getAuthority()
171
    {
172
        return ($this->getUserInfo() !== '') ? $this->getUserInfo().
173
        '@' ? ''.$this->getHost().($this->getPort() !== '') : ':'.$this->getPort() : '';
174
    }
175
176
    /**
177
     * Get the user information component of the URL.
178
     *
179
     * @since 1.0.0
180
     *
181
     * @return string
182
     */
183
    public function getUserInfo()
184
    {
185
        return $this->user;
186
    }
187
188
    /**
189
     * Get the host component of the URL.
190
     *
191
     * @since 1.0.0
192
     *
193
     * @return string
194
     */
195
    public function getHost()
196
    {
197
        return $this->host;
198
    }
199
200
    /**
201
     * Get the path component of the URL.
202
     *
203
     * @since 1.0.0
204
     *
205
     * @return string
206
     */
207
    public function getPath()
208
    {
209
        return $this->path;
210
    }
211
212
    /**
213
     * Get the port component of the URL.
214
     *
215
     * @since 1.0.0
216
     *
217
     * @return int
218
     */
219
    public function getPort()
220
    {
221
        return $this->port;
222
    }
223
224
    /**
225
     * Get the query string of the URL.
226
     *
227
     * @since 1.0.0
228
     *
229
     * @return string
230
     */
231
    public function getQuery()
232
    {
233
        return $this->query;
234
    }
235
236
    /**
237
     * Get the fragment component of the URL.
238
     *
239
     * @since 1.0.0
240
     *
241
     * @return string
242
     */
243
    public function getFragment()
244
    {
245
        return $this->fragment;
246
    }
247
248
    /**
249
     * Get the basePath segment component of the URL.
250
     *
251
     * @since 1.0.0
252
     *
253
     * @return string
254
     */
255
    public function getBasePath()
256
    {
257
        return $this->basePath;
258
    }
259
260
    /**
261
     * Return an instance with the specified user information.
262
     *
263
     * @param (string) $user username
264
     *                       (string) $password password
265
     *
266
     * @since 1.0.0
267
     *
268
     * @return object
269
     */
270
    public function withUserInfo($user, $password = null)
271
    {
272
        $this->user = $user;
273
        $this->password = $password;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Return an instance with the specified host.
280
     *
281
     * @param (string) $host valid host
282
     *
283
     * @since 1.0.0
284
     *
285
     * @return object
286
     */
287
    public function withHost($host)
288
    {
289
        $this->host = $host;
290
291
        return $this;
292
    }
293
294
    /**
295
     * Return an instance with the specified path.
296
     *
297
     * @param (string) $path valid path
298
     *
299
     * @since 1.0.0
300
     *
301
     * @return object
302
     */
303
    public function withPath($path)
304
    {
305
        // if the path is absolute, then clear basePath
306
        if (substr($path, 0, 1) == '/') {
307
            $this->basePath = '';
308
        }
309
        $this->path = $this->filterQuery($path);
310
311
        return $this;
312
    }
313
314
    /**
315
     * Return an instance with the specified port.
316
     *
317
     * @param (string) $port valid port
318
     *
319
     * @since 1.0.0
320
     *
321
     * @return object
322
     */
323
    public function withPort($port)
324
    {
325
        $this->port = $this->filterPort($port);
0 ignored issues
show
Bug introduced by
$port of type string is incompatible with the type integer expected by parameter $port of alphaz\http\Uri::filterPort(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

325
        $this->port = $this->filterPort(/** @scrutinizer ignore-type */ $port);
Loading history...
326
327
        return $this;
328
    }
329
330
    /**
331
     * Return an instance with the specified query.
332
     *
333
     * @param (string) $query valid query
334
     *
335
     * @since 1.0.0
336
     *
337
     * @return object
338
     */
339
    public function withQuery($query)
340
    {
341
        $this->query = $this->filterQuery(ltrim($query, '?'));
342
343
        return $this;
344
    }
345
346
    /**
347
     * Return an instance with the specified fragment.
348
     *
349
     * @param (string) $fragment valid fragment
350
     *
351
     * @since 1.0.0
352
     *
353
     * @return object
354
     */
355
    public function withFragment($fragment)
356
    {
357
        $this->fragment = $this->filterQuery(ltrim($fragment, '#'));
358
359
        return $this;
360
    }
361
362
    /**
363
     * Return an instance with the specified basePath segment.
364
     *
365
     * @param (string) $fragment valid fragment
366
     *
367
     * @since 1.0.0
368
     *
369
     * @return object
370
     */
371
    public function withBasePath($basePath)
372
    {
373
        $this->basePath = $this->filterQuery($basePath);
374
375
        return $this;
376
    }
377
378
    public function __toString()
379
    {
380
        $scheme = $this->getScheme();
381
        $authority = $this->getAuthority();
382
        $basePath = $this->getBasePath();
383
        $path = $this->getPath();
384
        $query = $this->getQuery();
385
        $fragment = $this->getFragment();
386
387
        $path = $basePath.'/'.ltrim($path, '/');
388
389
        return ($scheme !== '' ? $scheme.':' : '')
390
            .($authority !== '' ? '//'.$authority : '')
391
            .$path
392
            .($query !== '' ? '?'.$query : '')
393
            .($fragment !== '' ? '#'.$fragment : '');
394
    }
395
396
    /**
397
     * Get the fully qualified base URL.
398
     *
399
     * @since 1.0.0
400
     *
401
     * @return string
402
     */
403
    public function getBaseUrl()
404
    {
405
        $scheme = $this->getScheme();
406
        $authority = $this->getAuthority();
407
        $basePath = $this->getBasePath();
408
409
        if ($authority !== '' && substr($basePath, 0, 1) !== '/') {
410
            $basePath = $basePath.'/'.$basePath;
411
        }
412
413
        return ($scheme !== '' ? $scheme.':' : '')
414
            .($authority ? '//'.$authority : '')
415
            .rtrim($basePath, '/');
416
    }
417
}
418