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

nette / http / 22293514258

23 Feb 2026 04:58AM UTC coverage: 83.62%. Remained the same
22293514258

push

github

dg
added CLAUDE.md

924 of 1105 relevant lines covered (83.62%)

0.84 hits per line

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

98.18
/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 array_slice, explode, http_build_query, implode, ip2long, is_array, is_string, rawurlencode, str_starts_with, strrpos, substr;
12
use const PHP_QUERY_RFC3986;
13

14

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

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

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

57

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

67

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

76

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

82

83
        /** @deprecated */
84
        public function withUser(string $user): static
1✔
85
        {
86
                $dolly = clone $this;
1✔
87
                $dolly->user = $user;
1✔
88
                $dolly->authority = null;
1✔
89
                return $dolly;
1✔
90
        }
91

92

93
        /** @deprecated */
94
        public function getUser(): string
95
        {
96
                return $this->user;
1✔
97
        }
98

99

100
        /** @deprecated */
101
        public function withPassword(string $password): static
1✔
102
        {
103
                $dolly = clone $this;
1✔
104
                $dolly->password = $password;
1✔
105
                $dolly->authority = null;
1✔
106
                return $dolly;
1✔
107
        }
108

109

110
        /** @deprecated */
111
        public function getPassword(): string
112
        {
113
                return $this->password;
1✔
114
        }
115

116

117
        /** @deprecated */
118
        public function withoutUserInfo(): static
119
        {
120
                $dolly = clone $this;
1✔
121
                $dolly->user = $dolly->password = '';
1✔
122
                $dolly->authority = null;
1✔
123
                return $dolly;
1✔
124
        }
125

126

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

135

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

141

142
        public function getDomain(int $level = 2): string
1✔
143
        {
144
                $parts = ip2long($this->host)
1✔
145
                        ? [$this->host]
×
146
                        : explode('.', $this->host);
1✔
147
                $parts = $level >= 0
1✔
148
                        ? array_slice($parts, -$level)
1✔
149
                        : array_slice($parts, 0, $level);
1✔
150
                return implode('.', $parts);
1✔
151
        }
152

153

154
        public function withPort(int $port): static
1✔
155
        {
156
                $dolly = clone $this;
1✔
157
                $dolly->port = $port;
1✔
158
                $dolly->authority = null;
1✔
159
                return $dolly;
1✔
160
        }
161

162

163
        public function getPort(): ?int
164
        {
165
                return $this->port ?: $this->getDefaultPort();
1✔
166
        }
167

168

169
        public function getDefaultPort(): ?int
170
        {
171
                return Url::$defaultPorts[$this->scheme] ?? null;
1✔
172
        }
173

174

175
        public function withPath(string $path): static
1✔
176
        {
177
                return (clone $this)->setPath($path);
1✔
178
        }
179

180

181
        private function setPath(string $path): static
1✔
182
        {
183
                $this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
1✔
184
                return $this;
1✔
185
        }
186

187

188
        public function getPath(): string
189
        {
190
                return $this->path;
1✔
191
        }
192

193

194
        /** @param string|mixed[] $query */
195
        public function withQuery(string|array $query): static
1✔
196
        {
197
                $dolly = clone $this;
1✔
198
                $dolly->query = is_array($query) ? $query : Url::parseQuery($query);
1✔
199
                return $dolly;
1✔
200
        }
201

202

203
        public function getQuery(): string
204
        {
205
                return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
1✔
206
        }
207

208

209
        public function withQueryParameter(string $name, mixed $value): static
1✔
210
        {
211
                $dolly = clone $this;
1✔
212
                $dolly->query[$name] = $value;
1✔
213
                return $dolly;
1✔
214
        }
215

216

217
        /** @return mixed[] */
218
        public function getQueryParameters(): array
219
        {
220
                return $this->query;
1✔
221
        }
222

223

224
        /** @return mixed[]|string|null */
225
        public function getQueryParameter(string $name): array|string|null
1✔
226
        {
227
                return $this->query[$name] ?? null;
1✔
228
        }
229

230

231
        public function withFragment(string $fragment): static
1✔
232
        {
233
                $dolly = clone $this;
1✔
234
                $dolly->fragment = $fragment;
1✔
235
                return $dolly;
1✔
236
        }
237

238

239
        public function getFragment(): string
240
        {
241
                return $this->fragment;
1✔
242
        }
243

244

245
        /**
246
         * Returns the entire URI including query string and fragment.
247
         */
248
        public function getAbsoluteUrl(): string
249
        {
250
                return $this->getHostUrl() . $this->path
1✔
251
                        . (($tmp = $this->getQuery()) ? '?' . $tmp : '')
1✔
252
                        . ($this->fragment === '' ? '' : '#' . $this->fragment);
1✔
253
        }
254

255

256
        /**
257
         * Returns the [user[:pass]@]host[:port] part of URI.
258
         */
259
        public function getAuthority(): string
260
        {
261
                return $this->authority ??= $this->host === ''
1✔
262
                        ? ''
×
263
                        : ($this->user !== ''
1✔
264
                                ? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
1✔
265
                                : '')
1✔
266
                        . $this->host
1✔
267
                        . ($this->port && $this->port !== $this->getDefaultPort()
1✔
268
                                ? ':' . $this->port
1✔
269
                                : '');
1✔
270
        }
271

272

273
        /**
274
         * Returns the scheme and authority part of URI.
275
         */
276
        public function getHostUrl(): string
277
        {
278
                return ($this->scheme === '' ? '' : $this->scheme . ':')
1✔
279
                        . ($this->host === '' ? '' : '//' . $this->getAuthority());
1✔
280
        }
281

282

283
        public function __toString(): string
284
        {
285
                return $this->getAbsoluteUrl();
1✔
286
        }
287

288

289
        public function isEqual(string|Url|self $url): bool
1✔
290
        {
291
                return (new Url($this))->isEqual($url);
1✔
292
        }
293

294

295
        /**
296
         * Resolves relative URLs in the same way as browser. If path is relative, it is resolved against
297
         * base URL, if begins with /, it is resolved against the host root.
298
         */
299
        public function resolve(string $reference): self
1✔
300
        {
301
                $ref = new self($reference);
1✔
302
                if ($ref->scheme !== '') {
1✔
303
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
304
                        return $ref;
1✔
305
                }
306

307
                $ref->scheme = $this->scheme;
1✔
308

309
                if ($ref->host !== '') {
1✔
310
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
311
                        return $ref;
1✔
312
                }
313

314
                $ref->host = $this->host;
1✔
315
                $ref->port = $this->port;
1✔
316

317
                if ($ref->path === '') {
1✔
318
                        $ref->path = $this->path;
1✔
319
                        $ref->query = $ref->query ?: $this->query;
1✔
320
                } elseif (str_starts_with($ref->path, '/')) {
1✔
321
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
322
                } else {
323
                        $ref->path = Url::removeDotSegments($this->mergePath($ref->path));
1✔
324
                }
325
                return $ref;
1✔
326
        }
327

328

329
        /** @internal */
330
        protected function mergePath(string $path): string
1✔
331
        {
332
                $pos = strrpos($this->path, '/');
1✔
333
                return $pos === false ? $path : substr($this->path, 0, $pos + 1) . $path;
1✔
334
        }
335

336

337
        public function jsonSerialize(): string
338
        {
339
                return $this->getAbsoluteUrl();
1✔
340
        }
341

342

343
        /** @internal */
344
        final public function export(): array
345
        {
346
                return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
1✔
347
        }
348
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc