diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d6fcb0..97a11f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All Notable changes to `league-uri-parser` will be documented in this file ### Added +- `Parser::isValidHost` method - `ParserException::createFromInvalidScheme` replaces `ParserException::createFromInvalidState` usage - `ParserException::createFromInvalidPath` replaces `ParserException::createFromInvalidState` usage diff --git a/src/HostValidation.php b/src/HostValidation.php index e947196..9dbb548 100644 --- a/src/HostValidation.php +++ b/src/HostValidation.php @@ -37,15 +37,27 @@ trait HostValidation */ protected function filterHost($host) { - if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) - || $this->isValidHostnameIpv6($host) - || $this->isValidHostname($host)) { + if ($this->isValidHost($host)) { return $host; } throw ParserException::createFromInvalidHost($host); } + /** + * Tell whether a Host is valid + * + * @param string $host + * @return bool + */ + public function isValidHost($host) + { + return '' == $host + || filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) + || $this->isValidHostnameIpv6($host) + || $this->isValidHostname($host); + } + /** * validate an Ipv6 Hostname * diff --git a/test/ParserTest.php b/test/ParserTest.php index 1ea956d..69be7ca 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -609,7 +609,6 @@ public function testInvalidURI() 'invalid host and URI' => ['2620:0:1cfe:face:b00c::3'], 'invalid scheme and path' => ['0scheme://host/path?query#fragment'], 'invalid path PHP bug #72811' => ['[::1]:80'], - 'invalid uri with authority without host' => ['//user@:80'], ]; } }