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

nette / http / 11677741619

05 Nov 2024 04:45AM UTC coverage: 82.146% (+0.4%) from 81.742%
11677741619

push

github

dg
IRequest, IResponse: added typehints, unification (BC break)

2 of 2 new or added lines in 1 file covered. (100.0%)

13 existing lines in 2 files now uncovered.

911 of 1109 relevant lines covered (82.15%)

0.82 hits per line

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

98.26
/src/Http/UrlImmutable.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\Http;
11

12
use Nette;
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 $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
        private array $query = [];
52
        private string $fragment = '';
53
        private ?string $authority = null;
54

55

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

65

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

74

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

80

81
        /** @deprecated */
82
        public function withUser(string $user): static
1✔
83
        {
84
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1✔
85
                $dolly = clone $this;
1✔
86
                $dolly->user = $user;
1✔
87
                $dolly->authority = null;
1✔
88
                return $dolly;
1✔
89
        }
90

91

92
        /** @deprecated */
93
        public function getUser(): string
94
        {
95
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1✔
96
                return $this->user;
1✔
97
        }
98

99

100
        /** @deprecated */
101
        public function withPassword(string $password): static
1✔
102
        {
103
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1✔
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
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1✔
115
                return $this->password;
1✔
116
        }
117

118

119
        /** @deprecated */
120
        public function withoutUserInfo(): static
121
        {
122
                trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
1✔
123
                $dolly = clone $this;
1✔
124
                $dolly->user = $dolly->password = '';
1✔
125
                $dolly->authority = null;
1✔
126
                return $dolly;
1✔
127
        }
128

129

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

138

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

144

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

156

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

165

166
        public function getPort(): ?int
167
        {
168
                return $this->port ?: $this->getDefaultPort();
1✔
169
        }
170

171

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

177

178
        public function withPath(string $path): static
1✔
179
        {
180
                return (clone $this)->setPath($path);
1✔
181
        }
182

183

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

190

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

196

197
        public function withQuery(string|array $query): static
1✔
198
        {
199
                $dolly = clone $this;
1✔
200
                $dolly->query = is_array($query) ? $query : Url::parseQuery($query);
1✔
201
                return $dolly;
1✔
202
        }
203

204

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

210

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

218

219
        public function getQueryParameters(): array
220
        {
221
                return $this->query;
1✔
222
        }
223

224

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✔
UNCOV
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