Skip to content

Commit d74dc45

Browse files
committed
UrlImmutable, UrlScript: removed build() method
1 parent 2b99b0a commit d74dc45

File tree

2 files changed

+45
-54
lines changed

2 files changed

+45
-54
lines changed

src/Http/UrlImmutable.php

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class UrlImmutable implements \JsonSerializable
5050
private string $path = '';
5151
private array $query = [];
5252
private string $fragment = '';
53-
private string $authority = '';
53+
private ?string $authority = null;
5454

5555

5656
/**
@@ -60,15 +60,14 @@ public function __construct(string|self|Url $url)
6060
{
6161
$url = is_string($url) ? new Url($url) : $url;
6262
[$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment] = $url->export();
63-
$this->build();
6463
}
6564

6665

6766
public function withScheme(string $scheme): static
6867
{
6968
$dolly = clone $this;
7069
$dolly->scheme = $scheme;
71-
$dolly->build();
70+
$dolly->authority = null;
7271
return $dolly;
7372
}
7473

@@ -83,7 +82,7 @@ public function withUser(string $user): static
8382
{
8483
$dolly = clone $this;
8584
$dolly->user = $user;
86-
$dolly->build();
85+
$dolly->authority = null;
8786
return $dolly;
8887
}
8988

@@ -98,7 +97,7 @@ public function withPassword(string $password): static
9897
{
9998
$dolly = clone $this;
10099
$dolly->password = $password;
101-
$dolly->build();
100+
$dolly->authority = null;
102101
return $dolly;
103102
}
104103

@@ -113,7 +112,7 @@ public function withoutUserInfo(): static
113112
{
114113
$dolly = clone $this;
115114
$dolly->user = $dolly->password = '';
116-
$dolly->build();
115+
$dolly->authority = null;
117116
return $dolly;
118117
}
119118

@@ -122,8 +121,8 @@ public function withHost(string $host): static
122121
{
123122
$dolly = clone $this;
124123
$dolly->host = $host;
125-
$dolly->build();
126-
return $dolly;
124+
$dolly->authority = null;
125+
return $dolly->setPath($dolly->path);
127126
}
128127

129128

@@ -149,7 +148,7 @@ public function withPort(int $port): static
149148
{
150149
$dolly = clone $this;
151150
$dolly->port = $port;
152-
$dolly->build();
151+
$dolly->authority = null;
153152
return $dolly;
154153
}
155154

@@ -168,10 +167,14 @@ public function getDefaultPort(): ?int
168167

169168
public function withPath(string $path): static
170169
{
171-
$dolly = clone $this;
172-
$dolly->path = $path;
173-
$dolly->build();
174-
return $dolly;
170+
return (clone $this)->setPath($path);
171+
}
172+
173+
174+
private function setPath(string $path): static
175+
{
176+
$this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
177+
return $this;
175178
}
176179

177180

@@ -185,7 +188,6 @@ public function withQuery(string|array $query): static
185188
{
186189
$dolly = clone $this;
187190
$dolly->query = is_array($query) ? $query : Url::parseQuery($query);
188-
$dolly->build();
189191
return $dolly;
190192
}
191193

@@ -220,7 +222,6 @@ public function withFragment(string $fragment): static
220222
{
221223
$dolly = clone $this;
222224
$dolly->fragment = $fragment;
223-
$dolly->build();
224225
return $dolly;
225226
}
226227

@@ -247,7 +248,15 @@ public function getAbsoluteUrl(): string
247248
*/
248249
public function getAuthority(): string
249250
{
250-
return $this->authority;
251+
return $this->authority ??= $this->host === ''
252+
? ''
253+
: ($this->user !== ''
254+
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
255+
: '')
256+
. $this->host
257+
. ($this->port && $this->port !== $this->getDefaultPort()
258+
? ':' . $this->port
259+
: '');
251260
}
252261

253262

@@ -256,8 +265,8 @@ public function getAuthority(): string
256265
*/
257266
public function getHostUrl(): string
258267
{
259-
return ($this->scheme ? $this->scheme . ':' : '')
260-
. ($this->authority !== '' ? '//' . $this->authority : '');
268+
return ($this->scheme === '' ? '' : $this->scheme . ':')
269+
. ($this->host === '' ? '' : '//' . $this->getAuthority());
261270
}
262271

263272

@@ -284,22 +293,4 @@ final public function export(): array
284293
{
285294
return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
286295
}
287-
288-
289-
protected function build(): void
290-
{
291-
if ($this->host && !str_starts_with($this->path, '/')) {
292-
$this->path = '/' . $this->path;
293-
}
294-
295-
$this->authority = $this->host === ''
296-
? ''
297-
: ($this->user !== ''
298-
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
299-
: '')
300-
. $this->host
301-
. ($this->port && $this->port !== $this->getDefaultPort()
302-
? ':' . $this->port
303-
: '');
304-
}
305296
}

src/Http/UrlScript.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,30 @@ class UrlScript extends UrlImmutable
4040

4141
public function __construct(string|Url $url = '/', string $scriptPath = '')
4242
{
43-
$this->scriptPath = $scriptPath;
4443
parent::__construct($url);
45-
$this->build();
44+
$this->setScriptPath($scriptPath);
4645
}
4746

4847

4948
public function withPath(string $path, string $scriptPath = ''): static
5049
{
51-
$dolly = clone $this;
52-
$dolly->scriptPath = $scriptPath;
53-
$parent = UrlImmutable::withPath(...)->bindTo($dolly);
54-
return $parent($path);
50+
$dolly = parent::withPath($path);
51+
$dolly->setScriptPath($scriptPath);
52+
return $dolly;
53+
}
54+
55+
56+
private function setScriptPath(string $scriptPath): void
57+
{
58+
$path = $this->getPath();
59+
$scriptPath = $scriptPath ?: $path;
60+
$pos = strrpos($scriptPath, '/');
61+
if ($pos === false || strncmp($scriptPath, $path, $pos + 1)) {
62+
throw new Nette\InvalidArgumentException("ScriptPath '$scriptPath' doesn't match path '$path'");
63+
}
64+
65+
$this->scriptPath = $scriptPath;
66+
$this->basePath = substr($scriptPath, 0, $pos + 1);
5567
}
5668

5769

@@ -94,16 +106,4 @@ public function getPathInfo(): string
94106
}
95107

96108

97-
protected function build(): void
98-
{
99-
parent::build();
100-
$path = $this->getPath();
101-
$this->scriptPath = $this->scriptPath ?: $path;
102-
$pos = strrpos($this->scriptPath, '/');
103-
if ($pos === false || strncmp($this->scriptPath, $path, $pos + 1)) {
104-
throw new Nette\InvalidArgumentException("ScriptPath '$this->scriptPath' doesn't match path '$path'");
105-
}
106-
107-
$this->basePath = substr($this->scriptPath, 0, $pos + 1);
108-
}
109109
}

0 commit comments

Comments
 (0)