Browse Source

implement custom Address::get attributes

main
yggverse 8 months ago
parent
commit
0acff609f4
  1. 47
      src/Address.php

47
src/Address.php

@ -198,28 +198,37 @@ class Address
$this->_separator = $value; $this->_separator = $value;
} }
public function get(): string public function get(
bool $scheme = true,
bool $user = true,
bool $pass = true,
bool $host = true,
bool $port = true,
bool $path = true,
bool $query = true,
bool $fragment = true,
): string
{ {
$address = ''; $address = '';
if ($scheme = $this->getScheme()) if ($scheme && $this->getScheme())
{ {
$address .= sprintf( $address .= sprintf(
'%s:%s%s', '%s:%s%s',
$scheme, $this->getScheme(),
$this->getSeparator(), $this->getSeparator(),
$this->getSeparator() $this->getSeparator()
); );
} }
if ($user = $this->getUser()) if ($user && $this->getUser())
{ {
if ($pass = $this->getPass()) if ($pass && $this->getPass())
{ {
$address .= sprintf( $address .= sprintf(
'%s:%s@', '%s:%s@',
$user, $this->getUser(),
$pass $this->getPass()
); );
} }
@ -227,47 +236,47 @@ class Address
{ {
$address .= sprintf( $address .= sprintf(
'%s@', '%s@',
$user $this->getUser()
); );
} }
} }
if ($host = $this->getHost()) if ($host && $this->getHost())
{ {
$address .= $host; $address .= $this->getHost();
} }
if ($port = $this->getPort()) if ($port && $this->getPort())
{ {
$address .= sprintf( $address .= sprintf(
':%d', ':%d',
$port $this->getPort()
); );
} }
if ($path = $this->getPath()) if ($path && $this->getPath())
{ {
if (!str_starts_with($path, $this->getSeparator())) if (!str_starts_with($this->getPath(), $this->getSeparator()))
{ {
$address .= $this->getSeparator(); $address .= $this->getSeparator();
} }
$address .= $path; $address .= $this->getPath();
} }
if ($query = $this->getQuery()) if ($query && $this->getQuery())
{ {
$address .= sprintf( $address .= sprintf(
'?%s', '?%s',
$query $this->getQuery()
); );
} }
if ($fragment = $this->getFragment()) if ($fragment && $this->getFragment())
{ {
$address .= sprintf( $address .= sprintf(
'#%s', '#%s',
$fragment $this->getFragment()
); );
} }

Loading…
Cancel
Save