Completed
Push — master ( 4c14df...2efaf2 )
by ignace nyamagana
02:28
created

Http::createFromUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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\Contracts\UriInterface;
18
use League\Uri\Exceptions\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
    private 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 the environment.
91
     */
92 26
    public static function createFromServer(array $server): self
93
    {
94 26
        return new self(Uri::createFromServer($server));
95
    }
96
97
    /**
98
     * Create a new instance from a URI and a Base URI.
99
     *
100
     * The returned URI must be absolute.
101
     *
102
     * @param mixed $uri      the input URI to create
103
     * @param mixed $base_uri the base URI used for reference
104
     */
105 2
    public static function createFromBaseUri($uri, $base_uri = null): self
106
    {
107 2
        return new self(Uri::createFromBaseUri($uri, $base_uri));
108
    }
109
110
    /**
111
     * Create a new instance from a URI object.
112
     *
113
     * @param Psr7UriInterface|UriInterface $uri the input URI to create
114
     */
115 2
    public static function createFromUri($uri): self
116
    {
117 2
        return new self(Uri::createFromUri($uri));
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 186
    public function getScheme(): string
124
    {
125 186
        return (string) $this->uri->getScheme();
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 166
    public function getAuthority(): string
132
    {
133 166
        return (string) $this->uri->getAuthority();
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 76
    public function getUserInfo(): string
140
    {
141 76
        return (string) $this->uri->getUserInfo();
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 196
    public function getHost(): string
148
    {
149 196
        return (string) $this->uri->getHost();
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 76
    public function getPort(): ?int
156
    {
157 76
        return $this->uri->getPort();
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 170
    public function getPath(): string
164
    {
165 170
        return $this->uri->getPath();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 44
    public function getQuery(): string
172
    {
173 44
        return (string) $this->uri->getQuery();
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 24
    public function getFragment(): string
180
    {
181 24
        return (string) $this->uri->getFragment();
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 10
    public function withScheme($scheme): self
188
    {
189 10
        $scheme = $this->filterInput($scheme);
190 10
        if ('' === $scheme) {
191 10
            $scheme = null;
192
        }
193
194 10
        $uri = $this->uri->withScheme($scheme);
195 10
        if ($uri->getScheme() === $this->uri->getScheme()) {
196 2
            return $this;
197
        }
198
199 8
        return new self($uri);
200
    }
201
202
    /**
203
     * Safely stringify input when possible.
204
     *
205
     * @param mixed $str the value to evaluate as a string
206
     *
207
     * @throws SyntaxError if the submitted data can not be converted to string
208
     *
209
     * @return string|mixed
210
     */
211 94
    private function filterInput($str)
212
    {
213 94
        if (is_scalar($str) || method_exists($str, '__toString')) {
214 92
            return (string) $str;
215
        }
216
217 2
        return $str;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 8
    public function withUserInfo($user, $password = null): self
224
    {
225 8
        $user = $this->filterInput($user);
226 8
        if ('' === $user) {
227 8
            $user = null;
228
        }
229
230 8
        $uri = $this->uri->withUserInfo($user, $password);
231 8
        if ($uri->getUserInfo() === $this->uri->getUserInfo()) {
232 8
            return $this;
233
        }
234
235 2
        return new self($uri);
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 90
    public function withHost($host): self
242
    {
243 90
        $host = $this->filterInput($host);
244 90
        if ('' === $host) {
245 12
            $host = null;
246
        }
247
248 90
        $uri = $this->uri->withHost($host);
249 90
        if ($uri->getHost() === $this->uri->getHost()) {
250 88
            return $this;
251
        }
252
253 8
        return new self($uri);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 8
    public function withPort($port): self
260
    {
261 8
        $uri = $this->uri->withPort($port);
262 8
        if ($uri->getPort() === $this->uri->getPort()) {
263 8
            return $this;
264
        }
265
266 2
        return new self($uri);
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 38
    public function withPath($path): self
273
    {
274 38
        $uri = $this->uri->withPath($path);
275 30
        if ($uri->getPath() === $this->uri->getPath()) {
276 24
            return $this;
277
        }
278
279 26
        return new self($uri);
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285 22
    public function withQuery($query): self
286
    {
287 22
        $query = $this->filterInput($query);
288 22
        if ('' === $query) {
289 22
            $query = null;
290
        }
291
292 22
        $uri = $this->uri->withQuery($query);
293 22
        if ($uri->getQuery() === $this->uri->getQuery()) {
294 22
            return $this;
295
        }
296
297 4
        return new self($uri);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 22
    public function withFragment($fragment): self
304
    {
305 22
        $fragment = $this->filterInput($fragment);
306 22
        if ('' === $fragment) {
307 20
            $fragment = null;
308
        }
309
310 22
        $uri = $this->uri->withFragment($fragment);
311 22
        if ($uri->getFragment() === $this->uri->getFragment()) {
312 22
            return $this;
313
        }
314
315 2
        return new self($uri);
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321 48
    public function __toString(): string
322
    {
323 48
        return $this->uri->__toString();
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329 2
    public function jsonSerialize(): string
330
    {
331 2
        return $this->uri->__toString();
332
    }
333
}
334