Completed
Push — master ( 527d45...ffab82 )
by Joao
23s queued 10s
created

Uri::setQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ByJG\Util;
4
5
use Psr\Http\Message\UriInterface;
6
7
/**
8
 * Class Uri
9
 */
10
class Uri implements UriInterface
11
{
12
13
14
    private $scheme;
15
16
    public function withScheme($value)
17
    {
18
        $clone = clone $this;
19
        $clone->scheme = strtolower($value);
20
        return $clone;
21
    }
22
23
    public function getScheme()
24
    {
25
        return $this->scheme;
26
    }
27
28
    private $username;
29
    private $password;
30
31
    public function withUserInfo($user, $password = null)
32
    {
33
        $clone = clone $this;
34
        $clone->username = $user;
35
        $clone->password = $password;
36
        return $clone;
37
    }
38
39
    public function getUserInfo()
40
    {
41
        return $this->username
42
            . (!empty($this->password) ? ':' . rawurlencode($this->password) : '' );
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    public function getUsername()
49
    {
50
        return $this->username;
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56
    public function getPassword()
57
    {
58
        return $this->password;
59
    }
60
61
    private $host;
62
63
    public function withHost($value)
64
    {
65
        $clone = clone $this;
66
        $clone->host = $value;
67
        return $clone;
68
    }
69
70
    public function getHost()
71
    {
72
        return $this->host;
73
    }
74
75
    private $port;
76
77
    /**
78
     * @param int|string|null $value
79
     * @return $this
80
     */
81
    public function withPort($value)
82
    {
83
        $clone = clone $this;
84
        $clone->port = $value;
85
        return $clone;
86
    }
87
88
    public function getPort()
89
    {
90
        return $this->port;
91
    }
92
93
    private $path;
94
95
    public function withPath($value)
96
    {
97
        $clone = clone $this;
98
        $clone->path = $value;
99
        return $clone;
100
    }
101
102
    public function getPath()
103
    {
104
        return $this->path;
105
    }
106
107
    private $query = [];
108
109
    public function withQuery($query)
110
    {
111
        $clone = clone $this;
112
        $clone->setQuery($query);
113
        return $clone;
114
    }
115
116
    protected function setQuery($query)
117
    {
118
        parse_str($query, $this->query);
119
        return $this;
120
    }
121
122
123
    public function getQuery()
124
    {
125
        return http_build_query($this->query, null, "&", PHP_QUERY_RFC3986);
126
    }
127
128
    /**
129
     * @param string $key
130
     * @param string|array $value
131
     * @param bool $isEncoded
132
     * @return $this
133
     */
134
    public function withQueryKeyValue($key, $value, $isEncoded = false)
135
    {
136
        $clone = clone $this;
137
        $clone->query[$key] = ($isEncoded ? rawurldecode($value) : $value);
138
        return $clone;
139
    }
140
141
    /**
142
     * Not from UriInterface
143
     *
144
     * @param $key
145
     * @return string
146
     */
147
    public function getQueryPart($key)
148
    {
149
        return $this->getFromArray($this->query, $key);
150
    }
151
152
    private function getFromArray($array, $key)
153
    {
154
        if (isset($array[$key])) {
155
            return $array[$key];
156
        }
157
158
        return null;
159
    }
160
161
    private $fragment;
162
163
    public function getFragment()
164
    {
165
        return $this->fragment;
166
    }
167
168
    public function withFragment($fragment)
169
    {
170
        $clone = clone $this;
171
        $clone->fragment = $fragment;
172
        return $clone;
173
    }
174
175
    public function getAuthority()
176
    {
177
        return
178
            $this->concatSuffix($this->getUserInfo(), "@")
179
            . $this->getHost()
180
            . $this->concatPrefix(':', $this->getPort());
181
    }
182
183
    public function __toString()
184
    {
185
        return
186
            $this->concatSuffix($this->getScheme(), '://')
187
            . $this->getAuthority()
188
            . $this->getPath()
189
            . $this->concatPrefix('?', $this->getQuery())
190
            . $this->concatPrefix('#', $this->getFragment());
191
    }
192
193
    private function concatSuffix($str, $suffix)
194
    {
195
        if (!empty($str)) {
196
            $str = $str . $suffix;
197
        }
198
        return $str;
199
    }
200
201
    private function concatPrefix($prefix, $str)
202
    {
203
        if (!empty($str)) {
204
            $str = $prefix . $str;
205
        }
206
        return $str;
207
    }
208
209
    /**
210
     * @param string $uri
211
     */
212
    public function __construct($uri = null)
213
    {
214
        if (empty($uri)) {
215
            return;
216
        }
217
218
        $pattern = "/^"
219
            . "(?:(?P<scheme>\w+):\/\/)?"
220
            . "(?:(?P<user>\S+?):(?P<pass>\S+)@)?"
221
            . "(?:(?P<user2>\S+)@)?"
222
            . "(?:(?P<host>(?![A-Za-z]:)[\w\d\-]+(?:\.[\w\d\-]+)*))?"
223
            . "(?::(?P<port>[\d]+))?"
224
            . "(?P<path>([A-Za-z]:)?[^?#]+)?"
225
            . "(?:\?(?P<query>[^#]+))?"
226
            . "(?:#(?P<fragment>.*))?"
227
            . "$/";
228
        preg_match($pattern, $uri, $parsed);
229
230
        $user = $this->getFromArray($parsed, 'user');
231
        if (empty($user)) {
232
            $user = $this->getFromArray($parsed, 'user2');
233
        }
234
235
        $this->scheme = $this->getFromArray($parsed, 'scheme');
236
        $this->host = $this->getFromArray($parsed, 'host');
237
        $this->port = $this->getFromArray($parsed, 'port');
238
        $this->username = $user;
239
        $this->password = rawurldecode($this->getFromArray($parsed, 'pass'));
240
        $this->path = preg_replace('~^//~', '', $this->getFromArray($parsed, 'path'));
241
        $this->setQuery($this->getFromArray($parsed, 'query'));
242
        $this->fragment = $this->getFromArray($parsed, 'fragment');
243
    }
244
245
    public static function getInstanceFromString($uriString = null)
246
    {
247
        return new Uri($uriString);
248
    }
249
250
    public static function getInstanceFromUri(UriInterface $uri)
251
    {
252
        return self::getInstanceFromString((string)$uri);
253
    }
254
}
255