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

nette / http / 39956161725

23 Oct 2026 12:45PM UTC coverage: 82.117% (-1.4%) from 83.509%
39956161725

push

github

dg
uses #Deprecated wip

1024 of 1247 relevant lines covered (82.12%)

0.82 hits per line

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

99.01
/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
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1 ✔
88
                $dolly = clone $this;
1 ✔
89
                $dolly->user = $user;
1 ✔
90
                $dolly->authority = null;
1 ✔
91
                return $dolly;
1 ✔
92
        }
93

94

95
        #[\Deprecated]
96
        public function getUser(): string
97
        {
98
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1 ✔
99
                return $this->user;
1 ✔
100
        }
101

102

103
        #[\Deprecated]
104
        public function withPassword(string $password): static
105
        {
106
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1 ✔
107
                $dolly = clone $this;
1 ✔
108
                $dolly->password = $password;
1 ✔
109
                $dolly->authority = null;
1 ✔
110
                return $dolly;
1 ✔
111
        }
112

113

114
        #[\Deprecated]
115
        public function getPassword(): string
116
        {
117
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1 ✔
118
                return $this->password;
1 ✔
119
        }
120

121

122
        #[\Deprecated]
123
        public function withoutUserInfo(): static
124
        {
125
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1 ✔
126
                $dolly = clone $this;
1 ✔
127
                $dolly->user = $dolly->password = '';
1 ✔
128
                $dolly->authority = null;
1 ✔
129
                return $dolly;
1 ✔
130
        }
131

132

133
        public function withHost(string $host): static
134
        {
135
                $dolly = clone $this;
1 ✔
136
                $dolly->host = $host;
1 ✔
137
                $dolly->authority = null;
1 ✔
138
                return $dolly->setPath($dolly->path);
1 ✔
139
        }
140

141

142
        public function getHost(): string
143
        {
144
                return $this->host;
1 ✔
145
        }
146

147

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

163

164
        public function withPort(int $port): static
165
        {
166
                $dolly = clone $this;
1 ✔
167
                $dolly->port = $port;
1 ✔
168
                $dolly->authority = null;
1 ✔
169
                return $dolly;
1 ✔
170
        }
171

172

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

181

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

190

191
        public function withPath(string $path): static
192
        {
193
                return (clone $this)->setPath($path);
1 ✔
194
        }
195

196

197
        private function setPath(string $path): static
198
        {
199
                $this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
1 ✔
200
                return $this;
1 ✔
201
        }
202

203

204
        public function getPath(): string
205
        {
206
                return $this->path;
1 ✔
207
        }
208

209

210
        /** @param string|mixed[] $query */
211
        public function withQuery(string|array $query): static
212
        {
213
                $dolly = clone $this;
1 ✔
214
                $dolly->query = is_array($query) ? $query : Url::parseQuery($query);
1 ✔
215
                return $dolly;
1 ✔
216
        }
217

218

219
        public function getQuery(): string
220
        {
221
                return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
1 ✔
222
        }
223

224

225
        public function withQueryParameter(string $name, mixed $value): static
226
        {
227
                $dolly = clone $this;
1 ✔
228
                $dolly->query[$name] = $value;
1 ✔
229
                return $dolly;
1 ✔
230
        }
231

232

233
        /** @return mixed[] */
234
        public function getQueryParameters(): array
235
        {
236
                return $this->query;
1 ✔
237
        }
238

239

240
        /** @return mixed[]|string|null */
241
        public function getQueryParameter(string $name): array|string|null
242
        {
243
                return $this->query[$name] ?? null;
1 ✔
244
        }
245

246

247
        public function withFragment(string $fragment): static
248
        {
249
                $dolly = clone $this;
1 ✔
250
                $dolly->fragment = $fragment;
1 ✔
251
                return $dolly;
1 ✔
252
        }
253

254

255
        public function getFragment(): string
256
        {
257
                return $this->fragment;
1 ✔
258
        }
259

260

261
        public function getAbsoluteUrl(): string
262
        {
263
                return $this->getHostUrl() . $this->path
1 ✔
264
                        . (($tmp = $this->getQuery()) ? '?' . $tmp : '')
1 ✔
265
                        . ($this->fragment === '' ? '' : '#' . $this->fragment);
1 ✔
266
        }
267

268

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

285

286
        /**
287
         * Returns the scheme and authority part of URI.
288
         */
289
        public function getHostUrl(): string
290
        {
291
                return ($this->scheme === '' ? '' : $this->scheme . ':')
1 ✔
292
                        . ($this->host === '' ? '' : '//' . $this->getAuthority());
1 ✔
293
        }
294

295

296
        public function __toString(): string
297
        {
298
                return $this->getAbsoluteUrl();
1 ✔
299
        }
300

301

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

310

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

323
                $ref->scheme = $this->scheme;
1 ✔
324

325
                if ($ref->host !== '') {
1 ✔
326
                        $ref->path = Url::removeDotSegments($ref->path);
1 ✔
327
                        return $ref;
1 ✔
328
                }
329

330
                $ref->host = $this->host;
1 ✔
331
                $ref->port = $this->port;
1 ✔
332

333
                if ($ref->path === '') {
1 ✔
334
                        $ref->path = $this->path;
1 ✔
335
                        $ref->query = $ref->query ?: $this->query;
1 ✔
336
                } elseif (str_starts_with($ref->path, '/')) {
1 ✔
337
                        $ref->path = Url::removeDotSegments($ref->path);
1 ✔
338
                } else {
339
                        $ref->path = Url::removeDotSegments($this->mergePath($ref->path));
1 ✔
340
                }
341
                return $ref;
1 ✔
342
        }
343

344

345
        /** @internal */
346
        protected function mergePath(string $path): string
347
        {
348
                $pos = strrpos($this->path, '/');
1 ✔
349
                return $pos === false ? $path : substr($this->path, 0, $pos + 1) . $path;
1 ✔
350
        }
351

352

353
        public function jsonSerialize(): string
354
        {
355
                return $this->getAbsoluteUrl();
1 ✔
356
        }
357

358

359
        /**
360
         * @return array{string, string, string, string, ?int, string, mixed[], string}
361
         * @internal
362
         */
363
        final public function export(): array
364
        {
365
                return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
1 ✔
366
        }
367
}
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