Completed
Push — master ( 52f237...3f5bb7 )
by Joao
02:16
created

Uri::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
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
        $this->scheme = $value;
19
        return $this;
20
    }
21
22
    public function getScheme()
23
    {
24
        return $this->scheme;
25
    }
26
27
    private $username;
28
    private $password;
29
30
    public function withUserInfo($user, $password = null)
31
    {
32
        $this->username = $user;
33
        $this->password = $password;
34
        return $this;
35
    }
36
37
    public function getUserInfo()
38
    {
39
        return $this->username
40
            . (!empty($this->password) ? ':' . $this->password : '' );
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getUsername()
47
    {
48
        return $this->username;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getPassword()
55
    {
56
        return $this->password;
57
    }
58
59
    private $host;
60
61
    public function withHost($value)
62
    {
63
        $this->host = $value;
64
        return $this;
65
    }
66
67
    public function getHost()
68
    {
69
        return $this->host;
70
    }
71
72
    private $port;
73
74
    /**
75
     * @param int|string|null $value
76
     * @return $this
77
     */
78
    public function withPort($value)
79
    {
80
        $this->port = $value;
81
        return $this;
82
    }
83
84
    public function getPort()
85
    {
86
        return $this->port;
87
    }
88
89
    private $path;
90
91
    public function withPath($value)
92
    {
93
        $this->path = $value;
94
        return $this;
95
    }
96
97
    public function getPath()
98
    {
99
        return $this->path;
100
    }
101
102
    private $query = [];
103
104
    public function withQuery($query)
105
    {
106
        parse_str($query, $this->query);
107
        return $this;
108
    }
109
110
111
    public function getQuery()
112
    {
113
        return http_build_query($this->query);
114
    }
115
116
    /**
117
     * @param string $key
118
     * @param string|array $value
119
     * @param bool $encode
120
     * @return $this
121
     */
122
    public function withQueryKeyValue($key, $value, $encode = true)
123
    {
124
        $this->query[$key] = ($encode ? urlencode($value) : $value);
125
        return $this;
126
    }
127
128
    /**
129
     * Not from UriInterface
130
     *
131
     * @param $key
132
     * @return string
133
     */
134
    public function getQueryPart($key)
135
    {
136
        return $this->getFromArray($this->query, $key);
137
    }
138
139
    private function getFromArray($array, $key)
140
    {
141
        if (isset($array[$key])) {
142
            return $array[$key];
143
        }
144
145
        return null;
146
    }
147
148
    private $fragment;
149
150
    public function getFragment()
151
    {
152
        return $this->fragment;
153
    }
154
155
    public function withFragment($fragment)
156
    {
157
        $this->fragment = $fragment;
158
        return $this;
159
    }
160
161
    public function getAuthority()
162
    {
163
        return
164
            $this->concatSuffix($this->getUserInfo(), "@")
165
            . $this->getHost()
166
            . $this->concatPrefix(':', $this->getPort());
167
    }
168
169
    public function __toString()
170
    {
171
        return
172
            $this->concatSuffix($this->getScheme(), ':')
173
            . '//' . $this->getAuthority()
174
            . $this->getPath()
175
            . $this->concatPrefix('?', $this->getQuery())
176
            . $this->concatPrefix('#', $this->getFragment());
177
    }
178
179
    private function concatSuffix($str, $suffix)
180
    {
181
        if (!empty($str)) {
182
            $str = $str . $suffix;
183
        }
184
        return $str;
185
    }
186
187
    private function concatPrefix($prefix, $str)
188
    {
189
        if (!empty($str)) {
190
            $str = $prefix . $str;
191
        }
192
        return $str;
193
    }
194
195
    /**
196
     * @param string $uri
197
     */
198
    public function __construct($uri = null)
199
    {
200
        if (!empty($uri)) {
201
            $parsed = parse_url($uri);
202
203
            $this->withScheme($this->getFromArray($parsed, 'scheme'));
204
            $this->withHost($this->getFromArray($parsed, 'host'));
205
            $this->withPort($this->getFromArray($parsed, 'port'));
206
            $this->withUserInfo($this->getFromArray($parsed, 'user'), $this->getFromArray($parsed, 'pass'));
207
            $this->withPath(preg_replace('~^//~', '', $this->getFromArray($parsed, 'path')));
208
            $this->withQuery($this->getFromArray($parsed, 'query'));
209
            $this->withFragment($this->getFromArray($parsed, 'fragment'));
210
        }
211
    }
212
}
213