Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
new method Uri::getStandardPort() added
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Apr 12, 2020
1 parent 533e56b commit 3213f49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
use Sunrise\Uri\Component\Query;
use Sunrise\Uri\Component\Fragment;

/**
* Import functions
*/
use function getservbyname;

/**
* Uniform Resource Identifier
*
Expand Down Expand Up @@ -266,6 +271,30 @@ public function getPort() : ?int
return $this->port;
}

/**
* Gets standard port number associated with the URI scheme
*
* [!] It's not PSR-7 method.
*
* @return null|int
*/
public function getStandardPort() : ?int
{
$scheme = $this->getScheme();

$servicePort = getservbyname($scheme, 'tcp');
if (false !== $servicePort) {
return $servicePort;
}

$servicePort = getservbyname($scheme, 'udp');
if (false !== $servicePort) {
return $servicePort;
}

return null;
}

/**
* {@inheritDoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ public function testGetFragment()
$this->assertEquals('fragment', $uri->getFragment());
}

public function testGetStandardPort() : void
{
$uri = new Uri('http://host:3000');
$this->assertEquals(80, $uri->getStandardPort());

$uri = new Uri('https://host:3000');
$this->assertEquals(443, $uri->getStandardPort());

$uri = new Uri('ftp://host:3000');
$this->assertEquals(21, $uri->getStandardPort());
}

// Withers...

public function testWithScheme()
Expand Down

0 comments on commit 3213f49

Please sign in to comment.