Url::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AnthonySan95\UrlSigner\Url;
4
5
use Illuminate\Support\Arr;
6
use InvalidArgumentException;
7
use MalformedUrlException;
8
9
class Url {
10
11
    protected $parsedUrl;
12
13
    /**
14
     * Url constructor.
15
     * @param $url
16
     * @param array $parameters
17
     * @throws \Throwable
18
     */
19
    public function __construct($url, $parameters = []) {
20
        throw_if(empty($url), InvalidArgumentException::class);
21
22
        $this->parsedUrl = parse_url($url);
23
24
        throw_if(!is_array($this->parsedUrl), MalformedUrlException::class);
25
26
        if (isset($this->parsedUrl['query'])) {
27
            parse_str($this->parsedUrl['query'], $this->parsedUrl['query']);
28
        }
29
30
        if (isset($parameters)) {
31
            if (empty($this->parsedUrl['query'])) {
32
                $this->parsedUrl['query'] = [];
33
            }
34
35
            $this->parsedUrl['query'] += $parameters;
36
        }
37
38
        if (isset($this->parsedUrl['query'])) {
39
            ksort($this->parsedUrl['query']);
40
        }
41
    }
42
43
    /**
44
     * Get the scheme of this url or an empty string if unset.
45
     *
46
     * @return string
47
     */
48
    public function scheme() {
49
        return $this->parsedUrl['scheme'] ?? '';
50
    }
51
52
    /**
53
     * Get the host of this url or an empty string if unset.
54
     *
55
     * @return string
56
     */
57
    public function host() {
58
        return $this->parsedUrl['host'] ?? '';
59
    }
60
61
    /**
62
     * Get the port of this url or an empty string if unset.
63
     *
64
     * @return string
65
     */
66
    public function port() {
67
        return $this->parsedUrl['port'] ?? '';
68
    }
69
70
    /**
71
     * Get the user of this url or an empty string if unset.
72
     *
73
     * @return string
74
     */
75
    public function user() {
76
        return $this->parsedUrl['user'] ?? '';
77
    }
78
79
    /**
80
     * Get the pass of this url or an empty string if unset.
81
     *
82
     * @return string
83
     */
84
    public function pass() {
85
        return $this->parsedUrl['pass'] ?? '';
86
    }
87
88
    /**
89
     * Get the path of this url or an empty string if unset.
90
     *
91
     * @return string
92
     */
93
    public function path() {
94
        return $this->parsedUrl['path'] ?? '';
95
    }
96
97
    /**
98
     * Retrieve a query string item from the request.
99
     *
100
     * @param  string $key
101
     * @param  string $default
102
     * @return string|array|null
103
     */
104
    public function query($key = null, $default = null) {
105
        if (empty($this->parsedUrl['query'])) {
106
            if (!is_null($key)) {
107
                return $default;
108
            }
109
110
            return [];
111
        }
112
113
        if (is_null($key)) {
114
            return $this->parsedUrl['query'];
115
        }
116
117
        return $this->parsedUrl['query'][$key] ?? $default;
118
    }
119
120
    /**
121
     * Get the fragment of this url or an empty string if unset.
122
     *
123
     * @return string
124
     */
125
    public function fragment() {
126
        return $this->parsedUrl['fragment'] ?? '';
127
    }
128
129
    /**
130
     * Obtain a string made out of this Url.
131
     *
132
     * @param bool $withQuery
133
     * @return string
134
     */
135
    public function get($withQuery = true) {
136
        // Ex url. http://usr:[email protected]:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment
137
138
        return
139
            ($this->scheme() ? $this->scheme() . '://' : '') .
140
            $this->user() .
141
            ($this->pass() ? ':' . $this->pass() : '') .
142
            (($this->user() || $this->pass()) ? '@' : '') .
143
            $this->host() .
144
            ($this->port() ? ':' . $this->port() : '') .
145
            $this->path() .
146
            ($withQuery && $this->query() ? '?' . Arr::query($this->query()) : '') .
0 ignored issues
show
Bug introduced by
It seems like $this->query() targeting AnthonySan95\UrlSigner\Url\Url::query() can also be of type string; however, Illuminate\Support\Arr::query() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
            ($this->fragment() ? '#' . $this->fragment() : '');
148
    }
149
150
    /**
151
     * Obtain a string made out of this Url without queries.
152
     *
153
     * @return string
154
     */
155
    public function getWithoutQuery() {
156
        return $this->get(false);
157
    }
158
159
    /**
160
     * To string magic function, automatically called when casting to string.
161
     *
162
     * @return string
163
     */
164
    public function __toString() {
165
        return $this->get();
166
    }
167
}
168