Skip to content

Commit dc36171

Browse files
committed
UrlImmutable: added with***() methods
1 parent bc11219 commit dc36171

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

src/Http/UrlImmutable.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,72 @@ public function __construct($url)
8787
}
8888

8989

90+
/**
91+
* @return static
92+
*/
93+
public function withScheme(string $scheme)
94+
{
95+
$dolly = clone $this;
96+
$dolly->scheme = $scheme;
97+
$dolly->build();
98+
return $dolly;
99+
}
100+
101+
90102
public function getScheme(): string
91103
{
92104
return $this->scheme;
93105
}
94106

95107

108+
/**
109+
* @return static
110+
*/
111+
public function withUser(string $user)
112+
{
113+
$dolly = clone $this;
114+
$dolly->user = $user;
115+
$dolly->build();
116+
return $dolly;
117+
}
118+
119+
96120
public function getUser(): string
97121
{
98122
return $this->user;
99123
}
100124

101125

126+
/**
127+
* @return static
128+
*/
129+
public function withPassword(string $password)
130+
{
131+
$dolly = clone $this;
132+
$dolly->password = $password;
133+
$dolly->build();
134+
return $dolly;
135+
}
136+
137+
102138
public function getPassword(): string
103139
{
104140
return $this->password;
105141
}
106142

107143

144+
/**
145+
* @return static
146+
*/
147+
public function withHost(string $host)
148+
{
149+
$dolly = clone $this;
150+
$dolly->host = $host;
151+
$dolly->build();
152+
return $dolly;
153+
}
154+
155+
108156
public function getHost(): string
109157
{
110158
return $this->host;
@@ -119,18 +167,55 @@ public function getDomain(int $level = 2): string
119167
}
120168

121169

170+
/**
171+
* @return static
172+
*/
173+
public function withPort(int $port)
174+
{
175+
$dolly = clone $this;
176+
$dolly->port = $port;
177+
$dolly->build();
178+
return $dolly;
179+
}
180+
181+
122182
public function getPort(): ?int
123183
{
124184
return $this->port ?: (Url::$defaultPorts[$this->scheme] ?? null);
125185
}
126186

127187

188+
/**
189+
* @return static
190+
*/
191+
public function withPath(string $path)
192+
{
193+
$dolly = clone $this;
194+
$dolly->path = $path;
195+
$dolly->build();
196+
return $dolly;
197+
}
198+
199+
128200
public function getPath(): string
129201
{
130202
return $this->path;
131203
}
132204

133205

206+
/**
207+
* @param string|array $query
208+
* @return static
209+
*/
210+
public function withQuery($query)
211+
{
212+
$dolly = clone $this;
213+
$dolly->query = is_array($query) ? $query : Url::parseQuery($query);
214+
$dolly->build();
215+
return $dolly;
216+
}
217+
218+
134219
public function getQuery(): string
135220
{
136221
return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986);
@@ -152,6 +237,18 @@ public function getQueryParameter(string $name)
152237
}
153238

154239

240+
/**
241+
* @return static
242+
*/
243+
public function withFragment(string $fragment)
244+
{
245+
$dolly = clone $this;
246+
$dolly->fragment = $fragment;
247+
$dolly->build();
248+
return $dolly;
249+
}
250+
251+
155252
public function getFragment(): string
156253
{
157254
return $this->fragment;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Http\UrlImmutable;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
test(function () {
13+
$url = new UrlImmutable('http://username%3A:password%3A@hostn%61me:60/p%61th/script.php?%61rg=value#%61nchor');
14+
15+
$url = $url->withScheme('');
16+
Assert::same('//username%3A:password%3A@hostname:60/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
17+
18+
$url = $url->withUser('name');
19+
Assert::same('//name:password%3A@hostname:60/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
20+
21+
$url = $url->withPassword('secret');
22+
Assert::same('//name:secret@hostname:60/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
23+
24+
$url = $url->withHost('localhost');
25+
Assert::same('//name:secret@localhost:60/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
26+
27+
$url = $url->withPort(123);
28+
Assert::same('//name:secret@localhost:123/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
29+
30+
$url = $url->withFragment('hello');
31+
Assert::same('//name:secret@localhost:123/p%61th/script.php?arg=value#hello', $url->absoluteUrl);
32+
});
33+
34+
35+
test(function () {
36+
$url = new UrlImmutable('http://username%3A:password%3A@hostn%61me:60/p%61th/script.php?%61rg=value#%61nchor');
37+
38+
$url = $url->withPath('');
39+
Assert::same('/', $url->getPath());
40+
41+
$url = $url->withPath('/');
42+
Assert::same('/', $url->getPath());
43+
44+
$url = $url->withPath('x');
45+
Assert::same('/x', $url->getPath());
46+
47+
$url = $url->withPath('/x');
48+
Assert::same('/x', $url->getPath());
49+
50+
$url = $url->withPath('');
51+
Assert::same('/', $url->getPath());
52+
53+
$url = $url->withHost('')->withPath('');
54+
Assert::same('http://?arg=value#anchor', $url->absoluteUrl);
55+
56+
$url = $url->withPath('');
57+
Assert::same('', $url->getPath());
58+
});
59+
60+
61+
test(function () {
62+
$url = new UrlImmutable('http://hostname/path?arg=value');
63+
Assert::same('arg=value', $url->query);
64+
65+
$url = $url->withQuery(['arg3' => 'value3']);
66+
Assert::same('arg3=value3', $url->query);
67+
68+
$url = $url->withQuery([null]);
69+
Assert::same('http://hostname/path', $url->getAbsoluteUrl());
70+
});

0 commit comments

Comments
 (0)