Skip to content

Commit

Permalink
UrlImmutable, UrlScript: removed build() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 5, 2024
1 parent 7ffeee6 commit cb1bd41
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 54 deletions.
63 changes: 27 additions & 36 deletions src/Http/UrlImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class UrlImmutable implements \JsonSerializable
private string $path = '';
private array $query = [];
private string $fragment = '';
private string $authority = '';
private ?string $authority = null;


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


public function withScheme(string $scheme): static
{
$dolly = clone $this;
$dolly->scheme = $scheme;
$dolly->build();
$dolly->authority = null;
return $dolly;
}

Expand All @@ -84,7 +83,7 @@ public function withUser(string $user): static
{
$dolly = clone $this;
$dolly->user = $user;
$dolly->build();
$dolly->authority = null;
return $dolly;
}

Expand All @@ -101,7 +100,7 @@ public function withPassword(string $password): static
{
$dolly = clone $this;
$dolly->password = $password;
$dolly->build();
$dolly->authority = null;
return $dolly;
}

Expand All @@ -118,7 +117,7 @@ public function withoutUserInfo(): static
{
$dolly = clone $this;
$dolly->user = $dolly->password = '';
$dolly->build();
$dolly->authority = null;
return $dolly;
}

Expand All @@ -127,8 +126,8 @@ public function withHost(string $host): static
{
$dolly = clone $this;
$dolly->host = $host;
$dolly->build();
return $dolly;
$dolly->authority = null;
return $dolly->setPath($dolly->path);
}


Expand All @@ -154,7 +153,7 @@ public function withPort(int $port): static
{
$dolly = clone $this;
$dolly->port = $port;
$dolly->build();
$dolly->authority = null;
return $dolly;
}

Expand All @@ -173,10 +172,14 @@ public function getDefaultPort(): ?int

public function withPath(string $path): static
{
$dolly = clone $this;
$dolly->path = $path;
$dolly->build();
return $dolly;
return (clone $this)->setPath($path);
}


private function setPath(string $path): static
{
$this->path = $this->host && !str_starts_with($path, '/') ? '/' . $path : $path;
return $this;
}


Expand All @@ -190,7 +193,6 @@ public function withQuery(string|array $query): static
{
$dolly = clone $this;
$dolly->query = is_array($query) ? $query : Url::parseQuery($query);
$dolly->build();
return $dolly;
}

Expand Down Expand Up @@ -225,7 +227,6 @@ public function withFragment(string $fragment): static
{
$dolly = clone $this;
$dolly->fragment = $fragment;
$dolly->build();
return $dolly;
}

Expand All @@ -252,7 +253,15 @@ public function getAbsoluteUrl(): string
*/
public function getAuthority(): string
{
return $this->authority;
return $this->authority ??= $this->host === ''
? ''
: ($this->user !== ''
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
: '')
. $this->host
. ($this->port && $this->port !== $this->getDefaultPort()
? ':' . $this->port
: '');
}


Expand All @@ -261,8 +270,8 @@ public function getAuthority(): string
*/
public function getHostUrl(): string
{
return ($this->scheme ? $this->scheme . ':' : '')
. ($this->authority !== '' ? '//' . $this->authority : '');
return ($this->scheme === '' ? '' : $this->scheme . ':')
. ($this->host === '' ? '' : '//' . $this->getAuthority());
}


Expand All @@ -289,22 +298,4 @@ final public function export(): array
{
return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
}


protected function build(): void
{
if ($this->host && !str_starts_with($this->path, '/')) {
$this->path = '/' . $this->path;
}

$this->authority = $this->host === ''
? ''
: ($this->user !== ''
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
: '')
. $this->host
. ($this->port && $this->port !== $this->getDefaultPort()
? ':' . $this->port
: '');
}
}
36 changes: 18 additions & 18 deletions src/Http/UrlScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,30 @@ class UrlScript extends UrlImmutable

public function __construct(string|Url $url = '/', string $scriptPath = '')
{
$this->scriptPath = $scriptPath;
parent::__construct($url);
$this->build();
$this->setScriptPath($scriptPath);
}


public function withPath(string $path, string $scriptPath = ''): static
{
$dolly = clone $this;
$dolly->scriptPath = $scriptPath;
$parent = UrlImmutable::withPath(...)->bindTo($dolly);
return $parent($path);
$dolly = parent::withPath($path);
$dolly->setScriptPath($scriptPath);
return $dolly;
}


private function setScriptPath(string $scriptPath): void
{
$path = $this->getPath();
$scriptPath = $scriptPath ?: $path;
$pos = strrpos($scriptPath, '/');
if ($pos === false || strncmp($scriptPath, $path, $pos + 1)) {
throw new Nette\InvalidArgumentException("ScriptPath '$scriptPath' doesn't match path '$path'");
}

$this->scriptPath = $scriptPath;
$this->basePath = substr($scriptPath, 0, $pos + 1);
}


Expand Down Expand Up @@ -94,16 +106,4 @@ public function getPathInfo(): string
}


protected function build(): void
{
parent::build();
$path = $this->getPath();
$this->scriptPath = $this->scriptPath ?: $path;
$pos = strrpos($this->scriptPath, '/');
if ($pos === false || strncmp($this->scriptPath, $path, $pos + 1)) {
throw new Nette\InvalidArgumentException("ScriptPath '$this->scriptPath' doesn't match path '$path'");
}

$this->basePath = substr($this->scriptPath, 0, $pos + 1);
}
}

0 comments on commit cb1bd41

Please sign in to comment.