Skip to content

Commit

Permalink
Fixing issue with port always being returned in URI, especially in Au…
Browse files Browse the repository at this point in the history
…thority section. Port will only be returned if its is present and NOT a standard port number for scheme.
  • Loading branch information
brentscheffler committed May 29, 2023
1 parent 9a6e0b8 commit 7c656f4
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 337 deletions.
39 changes: 34 additions & 5 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class Uri implements UriInterface
{
/**
* Standard ports.
*
* @var array<array-key,int>
*/
protected array $standard_ports = [
"http" => 80,
"https" => 443
];

public function __construct(
protected ?string $scheme = null,
protected ?string $host = null,
Expand All @@ -19,6 +29,17 @@ public function __construct(
{
}

/**
* Get the standard port for the given scheme.
*
* @param string $scheme
* @return integer|null
*/
protected function getStandardPortForScheme(string $scheme): ?int
{
return $this->standard_ports[$scheme] ?? null;
}

/**
* @inheritDoc
*/
Expand All @@ -38,14 +59,14 @@ public function getAuthority(): string

$authority = "";

if( !empty($this->username) || !empty($this->password) ){
$authority .= \sprintf("%s:%s@", $this->username ?? "", $this->password ?? "");
if( $this->getUserInfo() ){
$authority .= ($this->getUserInfo() . "@");
}

$authority .= $this->host;

if( $this->port ){
$authority .= (":" . $this->port);
if( $this->getPort() ){
$authority .= (":" . $this->getPort());
}

return $authority;
Expand Down Expand Up @@ -82,7 +103,15 @@ public function getHost(): string
*/
public function getPort(): ?int
{
return $this->port;
if( $this->port ) {
if( $this->scheme && $this->getStandardPortForScheme($this->scheme) === $this->port ) {
return null;
}

return $this->port;
}

return null;
}

/**
Expand Down
Loading

0 comments on commit 7c656f4

Please sign in to comment.