diff --git a/composer.json b/composer.json index 733eb51..9d5a7c1 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,7 @@ "psr/http-message": "^1.0" }, "require-dev": { - "phpunit/phpunit": "7.5.6", - "php-http/psr7-integration-tests": "dev-master" + "phpunit/phpunit": "7.5.6" }, "provide": { "psr/http-message-implementation": "1.0" diff --git a/src/Uri.php b/src/Uri.php index 3572c39..680e484 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -23,6 +23,11 @@ use Sunrise\Uri\Component\Query; use Sunrise\Uri\Component\Fragment; +/** + * Import functions + */ +use function getservbyname; + /** * Uniform Resource Identifier * @@ -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} */ diff --git a/tests/UriIntegrationTest.php b/tests/UriIntegrationTest.php deleted file mode 100644 index a90afdd..0000000 --- a/tests/UriIntegrationTest.php +++ /dev/null @@ -1,14 +0,0 @@ -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()