• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / http / 42152022906

06 Nov 2026 02:27AM UTC coverage: 82.372% (+0.4%) from 82.005%
42152022906

push

github

dg
IRequest::getFile() accepts an array of keys like Request::getFile() (BC break) [Closes #214]

1028 of 1248 relevant lines covered (82.37%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.96
/src/Http/UrlImmutable.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Http;
9

10
use Nette;
11
use function is_array, is_string;
12

13

14
/**
15
 * Immutable representation of a URL.
16
 *
17
 * <pre>
18
 * scheme  user  password  host  port      path        query    fragment
19
 *   |      |      |        |      |        |            |         |
20
 * /--\   /--\ /------\ /-------\ /--\/------------\ /--------\ /------\
21
 * http://john:x0y17575@nette.org:8042/en/manual.php?name=param#fragment  <-- absoluteUrl
22
 * \______\__________________________/
23
 *     |               |
24
 *  hostUrl        authority
25
 * </pre>
26
 *
27
 * @property-read string $scheme
28
 * @property-read string $user
29
 * @property-read string $password
30
 * @property-read string $host
31
 * @property-read int $port
32
 * @property-read string $path
33
 * @property-read string $query
34
 * @property-read string $fragment
35
 * @property-read string $absoluteUrl
36
 * @property-read string $authority
37
 * @property-read string $hostUrl
38
 * @property-read array<string,mixed> $queryParameters
39
 */
40
class UrlImmutable implements \JsonSerializable
41
{
42
        use Nette\SmartObject;
43

44
        private string $scheme = '';
45
        private string $user = '';
46
        private string $password = '';
47
        private string $host = '';
48
        private ?int $port = null;
49
        private string $path = '';
50

51
        /** @var mixed[] */
52
        private array $query = [];
53
        private string $fragment = '';
54
        private ?string $authority = null;
55

56

57
        /**
58
         * @throws Nette\InvalidArgumentException if URL is malformed
59
         */
60
        public function __construct(string|self|Url $url)
61
        {
62
                $url = is_string($url) ? new Url($url) : $url;
1 ✔
63
                [
64
                        $this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment,
1 ✔
65
                ] = $url->export();
1 ✔
66
        }
1 ✔
67

68

69
        public function withScheme(string $scheme): static
70
        {
71
                $dolly = clone $this;
1 ✔
72
                $dolly->scheme = $scheme;
1 ✔
73
                $dolly->authority = null;
1 ✔
74
                return $dolly;
1 ✔
75
        }
76

77

78
        public function getScheme(): string
79
        {
80
                return $this->scheme;
1 ✔
81
        }
82

83

84
        #[\Deprecated]
85
        public function withUser(string $user): static
86
        {
87
                $dolly = clone $this;
1 ✔
88
                $dolly->user = $user;
1 ✔
89
                $dolly->authority = null;
1 ✔
90
                return $dolly;
1 ✔
91
        }
92

93

94
        #[\Deprecated]
95
        public function getUser(): string
96
        {
97
                return $this->user;
1 ✔
98
        }
99

100

101
        #[\Deprecated]
102
        public function withPassword(string $password): static
103
        {
104
                $dolly = clone $this;
1 ✔
105
                $dolly->password = $password;
1 ✔
106
                $dolly->authority = null;
1 ✔
107
                return $dolly;
1 ✔
108
        }
109

110

111
        #[\Deprecated]
112
        public function getPassword(): string
113
        {
114
                return $this->password;
1 ✔
115
        }
116

117

118
        #[\Deprecated]
119
        public function withoutUserInfo(): static
120
        {
121
                $dolly = clone $this;
1 ✔
122
                $dolly->user = $dolly->password = '';
1 ✔
123
                $dolly->authority = null;
1 ✔
124
                return $dolly;
1 ✔
125
        }
126

127

128
        public function withHost(string $host): static
129
        {
130
                $dolly = clone $this;
1 ✔
131
                $dolly->host = $host;
1 ✔
132
                $dolly->authority = null;
1 ✔
133
                return $dolly->setPath($dolly->path);
1 ✔
134
        }
135

136

137
        public function getHost(): string
138
        {
139
                return $this->host;
1 ✔
140
        }
141

142

143
        /**
144
         * Returns the specified number of rightmost domain labels (e.g. level 2 of 'www.nette.org' -> 'nette.org').
145
         * Negative values trim from the right instead.
146
         */
147
        public function getDomain(int $level = 2): string
148
        {
149
                $parts = filter_var(trim($this->host, '[]'), FILTER_VALIDATE_IP) !== false
1 ✔
150
                        ? [$this->host]
1 ✔
151
                        : explode('.', $this->host);
1 ✔
152
                $parts = $level >= 0
1 ✔
153
                        ? array_slice($parts, -$level)
1 ✔
154
                        : array_slice($parts, 0, $level);
1 ✔
155
                return implode('.', $parts);
1 ✔
156
        }
157

158

159
        public function withPort(int $port): static
160
        {
161
                $dolly = clone $this;
1 ✔
162
                $dolly->port = $port;
1 ✔
163
                $dolly->authority = null;
1 ✔
164
                return $dolly;
1 ✔
165
        }
166

167

168
        /**
169
         * Returns the port number, falling back to the default port for the scheme if not explicitly set.
170
         */
171
        public function getPort(): ?int
172
        {
173
                return $this->port ?: $this->getDefaultPort();
1 ✔
174
        }
175

176

177
        /**
178
         * Returns the default port for the current scheme, or null if the scheme is not recognized.
179
         */
180
        public function getDefaultPort(): ?int
181
        {
182
                return Url::$defaultPorts[$this->scheme] ?? null;
1 ✔
183
        }
184

185

186
        public function withPath(string $path): static
187
        {
188
                return (clone $this)->setPath($path);
1 ✔
189
        }
190

191

192
        private function setPath(string $path): static
193
        {
194
                $this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
1 ✔
195
                return $this;
1 ✔
196
        }
197

198

199
        public function getPath(): string
200
        {
201
                return $this->path;
1 ✔
202
        }
203

204

205
        /** @param string|mixed[] $query */
206
        public function withQuery(string|array $query): static
207
        {
208
                $dolly = clone $this;
1 ✔
209
                $dolly->query = is_array($query) ? $query : Url::parseQuery($query);
1 ✔
210
                return $dolly;
1 ✔
211
        }
212

213

214
        public function getQuery(): string
215
        {
216
                return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
1 ✔
217
        }
218

219

220
        public function withQueryParameter(string $name, mixed $value): static
221
        {
222
                $dolly = clone $this;
1 ✔
223
                $dolly->query[$name] = $value;
1 ✔
224
                return $dolly;
1 ✔
225
        }
226

227

228
        /** @return mixed[] */
229
        public function getQueryParameters(): array
230
        {
231
                return $this->query;
1 ✔
232
        }
233

234

235
        /** @return mixed[]|string|null */
236
        public function getQueryParameter(string $name): array|string|null
237
        {
238
                return $this->query[$name] ?? null;
1 ✔
239
        }
240

241

242
        public function withFragment(string $fragment): static
243
        {
244
                $dolly = clone $this;
1 ✔
245
                $dolly->fragment = $fragment;
1 ✔
246
                return $dolly;
1 ✔
247
        }
248

249

250
        public function getFragment(): string
251
        {
252
                return $this->fragment;
1 ✔
253
        }
254

255

256
        public function getAbsoluteUrl(): string
257
        {
258
                return $this->getHostUrl() . $this->path
1 ✔
259
                        . (($tmp = $this->getQuery()) ? '?' . $tmp : '')
1 ✔
260
                        . ($this->fragment === '' ? '' : '#' . $this->fragment);
1 ✔
261
        }
262

263

264
        /**
265
         * Returns the [user[:pass]@]host[:port] part of URI.
266
         */
267
        public function getAuthority(): string
268
        {
269
                return $this->authority ??= $this->host === ''
1 ✔
270
                        ? ''
×
271
                        : ($this->user !== ''
1 ✔
272
                                ? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
1 ✔
273
                                : '')
1 ✔
274
                                . $this->host
1 ✔
275
                                . ($this->port && $this->port !== $this->getDefaultPort()
1 ✔
276
                                        ? ':' . $this->port
1 ✔
277
                                        : '');
1 ✔
278
        }
279

280

281
        /**
282
         * Returns the scheme and authority part of URI.
283
         */
284
        public function getHostUrl(): string
285
        {
286
                return ($this->scheme === '' ? '' : $this->scheme . ':')
1 ✔
287
                        . ($this->host === '' ? '' : '//' . $this->getAuthority());
1 ✔
288
        }
289

290

291
        public function __toString(): string
292
        {
293
                return $this->getAbsoluteUrl();
1 ✔
294
        }
295

296

297
        /**
298
         * Checks whether two URLs are equal, ignoring query parameter order and trailing dots in hostnames.
299
         */
300
        public function isEqual(string|Url|self $url): bool
301
        {
302
                return new Url($this)->isEqual($url);
1 ✔
303
        }
304

305

306
        /**
307
         * Resolves a URI reference against this URL the same way a browser would.
308
         * Relative paths are resolved against the current path; paths starting with / are resolved against the host root.
309
         */
310
        public function resolve(string $reference): self
311
        {
312
                $ref = new self($reference);
1 ✔
313
                if ($ref->scheme !== '') {
1 ✔
314
                        $ref->path = Url::removeDotSegments($ref->path);
1 ✔
315
                        return $ref;
1 ✔
316
                }
317

318
                $ref->scheme = $this->scheme;
1 ✔
319

320
                if ($ref->host !== '') {
1 ✔
321
                        $ref->path = Url::removeDotSegments($ref->path);
1 ✔
322
                        return $ref;
1 ✔
323
                }
324

325
                $ref->host = $this->host;
1 ✔
326
                $ref->port = $this->port;
1 ✔
327

328
                if ($ref->path === '') {
1 ✔
329
                        $ref->path = $this->path;
1 ✔
330
                        $ref->query = $ref->query ?: $this->query;
1 ✔
331
                } elseif (str_starts_with($ref->path, '/')) {
1 ✔
332
                        $ref->path = Url::removeDotSegments($ref->path);
1 ✔
333
                } else {
334
                        $ref->path = Url::removeDotSegments($this->mergePath($ref->path));
1 ✔
335
                }
336
                return $ref;
1 ✔
337
        }
338

339

340
        /** @internal */
341
        protected function mergePath(string $path): string
342
        {
343
                $pos = strrpos($this->path, '/');
1 ✔
344
                return $pos === false ? $path : substr($this->path, 0, $pos + 1) . $path;
1 ✔
345
        }
346

347

348
        public function jsonSerialize(): string
349
        {
350
                return $this->getAbsoluteUrl();
1 ✔
351
        }
352

353

354
        /**
355
         * @return array{string, string, string, string, ?int, string, mixed[], string}
356
         * @internal
357
         */
358
        final public function export(): array
359
        {
360
                return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
1 ✔
361
        }
362
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc