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

nette / http / 12735233527

12 Jan 2025 04:27PM UTC coverage: 83.456%. Remained the same
12735233527

push

github

dg
FileUpload::__construct() accepts path

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

20 existing lines in 3 files now uncovered.

908 of 1088 relevant lines covered (83.46%)

0.83 hits per line

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

98.18
/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
        public function withUser(string $user): static
1✔
82
        {
83
                $dolly = clone $this;
1✔
84
                $dolly->user = $user;
1✔
85
                $dolly->authority = null;
1✔
86
                return $dolly;
1✔
87
        }
88

89

90
        public function getUser(): string
91
        {
92
                return $this->user;
1✔
93
        }
94

95

96
        public function withPassword(string $password): static
1✔
97
        {
98
                $dolly = clone $this;
1✔
99
                $dolly->password = $password;
1✔
100
                $dolly->authority = null;
1✔
101
                return $dolly;
1✔
102
        }
103

104

105
        public function getPassword(): string
106
        {
107
                return $this->password;
1✔
108
        }
109

110

111
        public function withoutUserInfo(): static
112
        {
113
                $dolly = clone $this;
1✔
114
                $dolly->user = $dolly->password = '';
1✔
115
                $dolly->authority = null;
1✔
116
                return $dolly;
1✔
117
        }
118

119

120
        public function withHost(string $host): static
1✔
121
        {
122
                $dolly = clone $this;
1✔
123
                $dolly->host = $host;
1✔
124
                $dolly->authority = null;
1✔
125
                return $dolly->setPath($dolly->path);
1✔
126
        }
127

128

129
        public function getHost(): string
130
        {
131
                return $this->host;
1✔
132
        }
133

134

135
        public function getDomain(int $level = 2): string
1✔
136
        {
137
                $parts = ip2long($this->host)
1✔
UNCOV
138
                        ? [$this->host]
×
139
                        : explode('.', $this->host);
1✔
140
                $parts = $level >= 0
1✔
141
                        ? array_slice($parts, -$level)
1✔
142
                        : array_slice($parts, 0, $level);
1✔
143
                return implode('.', $parts);
1✔
144
        }
145

146

147
        public function withPort(int $port): static
1✔
148
        {
149
                $dolly = clone $this;
1✔
150
                $dolly->port = $port;
1✔
151
                $dolly->authority = null;
1✔
152
                return $dolly;
1✔
153
        }
154

155

156
        public function getPort(): ?int
157
        {
158
                return $this->port ?: $this->getDefaultPort();
1✔
159
        }
160

161

162
        public function getDefaultPort(): ?int
163
        {
164
                return Url::$defaultPorts[$this->scheme] ?? null;
1✔
165
        }
166

167

168
        public function withPath(string $path): static
1✔
169
        {
170
                return (clone $this)->setPath($path);
1✔
171
        }
172

173

174
        private function setPath(string $path): static
1✔
175
        {
176
                $this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
1✔
177
                return $this;
1✔
178
        }
179

180

181
        public function getPath(): string
182
        {
183
                return $this->path;
1✔
184
        }
185

186

187
        public function withQuery(string|array $query): static
1✔
188
        {
189
                $dolly = clone $this;
1✔
190
                $dolly->query = is_array($query) ? $query : Url::parseQuery($query);
1✔
191
                return $dolly;
1✔
192
        }
193

194

195
        public function getQuery(): string
196
        {
197
                return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
1✔
198
        }
199

200

201
        public function withQueryParameter(string $name, mixed $value): static
1✔
202
        {
203
                $dolly = clone $this;
1✔
204
                $dolly->query[$name] = $value;
1✔
205
                return $dolly;
1✔
206
        }
207

208

209
        public function getQueryParameters(): array
210
        {
211
                return $this->query;
1✔
212
        }
213

214

215
        public function getQueryParameter(string $name): array|string|null
1✔
216
        {
217
                return $this->query[$name] ?? null;
1✔
218
        }
219

220

221
        public function withFragment(string $fragment): static
1✔
222
        {
223
                $dolly = clone $this;
1✔
224
                $dolly->fragment = $fragment;
1✔
225
                return $dolly;
1✔
226
        }
227

228

229
        public function getFragment(): string
230
        {
231
                return $this->fragment;
1✔
232
        }
233

234

235
        /**
236
         * Returns the entire URI including query string and fragment.
237
         */
238
        public function getAbsoluteUrl(): string
239
        {
240
                return $this->getHostUrl() . $this->path
1✔
241
                        . (($tmp = $this->getQuery()) ? '?' . $tmp : '')
1✔
242
                        . ($this->fragment === '' ? '' : '#' . $this->fragment);
1✔
243
        }
244

245

246
        /**
247
         * Returns the [user[:pass]@]host[:port] part of URI.
248
         */
249
        public function getAuthority(): string
250
        {
251
                return $this->authority ??= $this->host === ''
1✔
UNCOV
252
                        ? ''
×
253
                        : ($this->user !== ''
1✔
254
                                ? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
1✔
255
                                : '')
1✔
256
                        . $this->host
1✔
257
                        . ($this->port && $this->port !== $this->getDefaultPort()
1✔
258
                                ? ':' . $this->port
1✔
259
                                : '');
1✔
260
        }
261

262

263
        /**
264
         * Returns the scheme and authority part of URI.
265
         */
266
        public function getHostUrl(): string
267
        {
268
                return ($this->scheme === '' ? '' : $this->scheme . ':')
1✔
269
                        . ($this->host === '' ? '' : '//' . $this->getAuthority());
1✔
270
        }
271

272

273
        public function __toString(): string
274
        {
275
                return $this->getAbsoluteUrl();
1✔
276
        }
277

278

279
        public function isEqual(string|Url|self $url): bool
1✔
280
        {
281
                return (new Url($this))->isEqual($url);
1✔
282
        }
283

284

285
        /**
286
         * Resolves relative URLs in the same way as browser. If path is relative, it is resolved against
287
         * base URL, if begins with /, it is resolved against the host root.
288
         */
289
        public function resolve(string $reference): self
1✔
290
        {
291
                $ref = new self($reference);
1✔
292
                if ($ref->scheme !== '') {
1✔
293
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
294
                        return $ref;
1✔
295
                }
296

297
                $ref->scheme = $this->scheme;
1✔
298

299
                if ($ref->host !== '') {
1✔
300
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
301
                        return $ref;
1✔
302
                }
303

304
                $ref->host = $this->host;
1✔
305
                $ref->port = $this->port;
1✔
306

307
                if ($ref->path === '') {
1✔
308
                        $ref->path = $this->path;
1✔
309
                        $ref->query = $ref->query ?: $this->query;
1✔
310
                } elseif (str_starts_with($ref->path, '/')) {
1✔
311
                        $ref->path = Url::removeDotSegments($ref->path);
1✔
312
                } else {
313
                        $ref->path = Url::removeDotSegments($this->mergePath($ref->path));
1✔
314
                }
315
                return $ref;
1✔
316
        }
317

318

319
        /** @internal */
320
        protected function mergePath(string $path): string
1✔
321
        {
322
                $pos = strrpos($this->path, '/');
1✔
323
                return $pos === false ? $path : substr($this->path, 0, $pos + 1) . $path;
1✔
324
        }
325

326

327
        public function jsonSerialize(): string
328
        {
329
                return $this->getAbsoluteUrl();
1✔
330
        }
331

332

333
        /** @internal */
334
        final public function export(): array
335
        {
336
                return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
1✔
337
        }
338
}
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