From 533e56b14cc46a1b11b494a2506ddb6d4a681a87 Mon Sep 17 00:00:00 2001 From: "@fenric" Date: Sun, 12 Apr 2020 21:37:22 +0300 Subject: [PATCH 1/2] unreliable vendor package removed --- composer.json | 3 +-- tests/UriIntegrationTest.php | 14 -------------- 2 files changed, 1 insertion(+), 16 deletions(-) delete mode 100644 tests/UriIntegrationTest.php 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/tests/UriIntegrationTest.php b/tests/UriIntegrationTest.php deleted file mode 100644 index a90afdd..0000000 --- a/tests/UriIntegrationTest.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Sun, 12 Apr 2020 21:43:12 +0300 Subject: [PATCH 2/2] new method Uri::getStandardPort() added --- src/Uri.php | 29 +++++++++++++++++++++++++++++ tests/UriTest.php | 12 ++++++++++++ 2 files changed, 41 insertions(+) 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/UriTest.php b/tests/UriTest.php index 5246c6f..3f21e26 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -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()