Completed
Pull Request — master (#127)
by ignace nyamagana
02:19
created

Http::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Uri;
15
16
use JsonSerializable;
17
use League\Uri\Contract\UriInterface;
18
use League\Uri\Exception\SyntaxError;
19
use Psr\Http\Message\UriInterface as Psr7UriInterface;
20
use function is_scalar;
21
use function method_exists;
22
use function sprintf;
23
24
final class Http implements Psr7UriInterface, JsonSerializable
25
{
26
    /**
27
     * @var UriInterface
28
     */
29
    private $uri;
30
31
    /**
32
     * New instance.
33
     */
34 222
    public function __construct(UriInterface $uri)
35
    {
36 222
        $this->validate($uri);
37 222
        $this->uri = $uri;
38 222
    }
39
40
    /**
41
     * Validate the submitted uri against PSR-7 UriInterface.
42
     *
43
     * @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules
44
     */
45 228
    private function validate(UriInterface $uri): void
46
    {
47 228
        $scheme = $uri->getScheme();
48 228
        if (null === $scheme && '' === $uri->getHost()) {
49 2
            throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri));
50
        }
51
52 228
        $port = $uri->getPort();
53 228
        if (null !== $port && ($port < 0 || $port > 65535)) {
54 2
            throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort()));
55
        }
56 228
    }
57
58
    /**
59
     * Static method called by PHP's var export.
60
     *
61
     * @return static
62
     */
63 18
    public static function __set_state(array $components): self
64
    {
65 18
        return new self($components['uri']);
66
    }
67
68
    /**
69
     * Create a new instance from a string.
70
     *
71
     * @param string|mixed $uri
72
     */
73 200
    public static function createFromString($uri = ''): self
74
    {
75 200
        return new self(Uri::createFromString($uri));
76
    }
77
78
    /**
79
     * Create a new instance from a hash of parse_url parts.
80
     *
81
     * @param array $components a hash representation of the URI similar
82
     *                          to PHP parse_url function result
83
     */
84 2
    public static function createFromComponents(array $components): self
85
    {
86 2
        return new self(Uri::createFromComponents($components));
87
    }
88
89
    /**
90
     * Create a new instance from a URI and a Base URI.
91
     *
92
     * The returned URI must be absolute.
93
     *
94
     * @param mixed $uri      the input URI to create
95
     * @param mixed $base_uri the base URI used for reference
96
     */
97 2
    public static function create($uri, $base_uri = null): self
98
    {
99 2
        return new self(Uri::create($uri, $base_uri));
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 186
    public function getScheme(): string
106
    {
107 186
        return (string) $this->uri->getScheme();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 166
    public function getAuthority(): string
114
    {
115 166
        return (string) $this->uri->getAuthority();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 76
    public function getUserInfo(): string
122
    {
123 76
        return (string) $this->uri->getUserInfo();
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 196
    public function getHost(): string
130
    {
131 196
        return (string) $this->uri->getHost();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 76
    public function getPort(): ?int
138
    {
139 76
        return $this->uri->getPort();
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 170
    public function getPath(): string
146
    {
147 170
        return $this->uri->getPath();
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 44
    public function getQuery(): string
154
    {
155 44
        return (string) $this->uri->getQuery();
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 24
    public function getFragment(): string
162
    {
163 24
        return (string) $this->uri->getFragment();
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 10
    public function withScheme($scheme): self
170
    {
171 10
        $scheme = $this->filterInput($scheme);
172 10
        if ('' === $scheme) {
173 10
            $scheme = null;
174
        }
175
176 10
        $uri = $this->uri->withScheme($scheme);
177 10
        if ($uri->getScheme() === $this->uri->getScheme()) {
178 2
            return $this;
179
        }
180
181 8
        return new self($uri);
182
    }
183
184
    /**
185
     * Safely stringify input when possible.
186
     *
187
     * @param mixed $str the value to evaluate as a string
188
     *
189
     * @throws SyntaxError if the submitted data can not be converted to string
190
     *
191
     * @return string|mixed
192
     */
193 94
    private function filterInput($str)
194
    {
195 94
        if (is_scalar($str) || method_exists($str, '__toString')) {
196 92
            return (string) $str;
197
        }
198
199 2
        return $str;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 8
    public function withUserInfo($user, $password = null): self
206
    {
207 8
        $user = $this->filterInput($user);
208 8
        if ('' === $user) {
209 8
            $user = null;
210
        }
211
212 8
        $uri = $this->uri->withUserInfo($user, $password);
213 8
        if ($uri->getUserInfo() === $this->uri->getUserInfo()) {
214 8
            return $this;
215
        }
216
217 2
        return new self($uri);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 90
    public function withHost($host): self
224
    {
225 90
        $host = $this->filterInput($host);
226 90
        if ('' === $host) {
227 12
            $host = null;
228
        }
229
230 90
        $uri = $this->uri->withHost($host);
231 90
        if ($uri->getHost() === $this->uri->getHost()) {
232 88
            return $this;
233
        }
234
235 8
        return new self($uri);
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 8
    public function withPort($port): self
242
    {
243 8
        $uri = $this->uri->withPort($port);
244 8
        if ($uri->getPort() === $this->uri->getPort()) {
245 8
            return $this;
246
        }
247
248 2
        return new self($uri);
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254 38
    public function withPath($path): self
255
    {
256 38
        $uri = $this->uri->withPath($path);
257 30
        if ($uri->getPath() === $this->uri->getPath()) {
258 24
            return $this;
259
        }
260
261 26
        return new self($uri);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 22
    public function withQuery($query): self
268
    {
269 22
        $query = $this->filterInput($query);
270 22
        if ('' === $query) {
271 22
            $query = null;
272
        }
273
274 22
        $uri = $this->uri->withQuery($query);
275 22
        if ($uri->getQuery() === $this->uri->getQuery()) {
276 22
            return $this;
277
        }
278
279 4
        return new self($uri);
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285 22
    public function withFragment($fragment): self
286
    {
287 22
        $fragment = $this->filterInput($fragment);
288 22
        if ('' === $fragment) {
289 20
            $fragment = null;
290
        }
291
292 22
        $uri = $this->uri->withFragment($fragment);
293 22
        if ($uri->getFragment() === $this->uri->getFragment()) {
294 22
            return $this;
295
        }
296
297 2
        return new self($uri);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 48
    public function __toString(): string
304
    {
305 48
        return $this->uri->__toString();
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311 2
    public function jsonSerialize(): string
312
    {
313 2
        return $this->uri->__toString();
314
    }
315
}
316