diff --git a/CHANGELOG.md b/CHANGELOG.md index 006247c..1fe8c5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 2.0.0@beta2 + +### Added + +* DsnParser::parseUrl(string $dsn): Url +* DsnParser::parsePath(string $dsn): Path + ## 2.0.0@beta1 Version 2 comes with a new definition was a DSN really is. It supports functions diff --git a/src/DsnParser.php b/src/DsnParser.php index ae72801..2c48886 100644 --- a/src/DsnParser.php +++ b/src/DsnParser.php @@ -8,6 +8,7 @@ use Nyholm\Dsn\Configuration\DsnFunction; use Nyholm\Dsn\Configuration\Path; use Nyholm\Dsn\Configuration\Url; +use Nyholm\Dsn\Exception\DsnTypeNotSupported; use Nyholm\Dsn\Exception\FunctionsNotAllowedException; use Nyholm\Dsn\Exception\SyntaxException; @@ -72,6 +73,26 @@ public static function parse(string $dsn): Dsn return self::getDsn($dsn); } + public static function parseUrl(string $dsn): Url + { + $dsn = self::parse($dsn); + if (!$dsn instanceof Url) { + throw DsnTypeNotSupported::onlyUrl($dsn); + } + + return $dsn; + } + + public static function parsePath(string $dsn): Path + { + $dsn = self::parse($dsn); + if (!$dsn instanceof Path) { + throw DsnTypeNotSupported::onlyPath($dsn); + } + + return $dsn; + } + /** * @return DsnFunction|Dsn */ @@ -117,18 +138,18 @@ private static function getDsn(string $dsn): Dsn ]; if ('?' === $matches[3][0]) { - $parts = self::parseUrl('http://localhost'.$matches[3], $dsn); + $parts = self::explodeUrl('http://localhost'.$matches[3], $dsn); return new Dsn($scheme, self::getQuery($parts)); } if ('/' === $matches[3][0]) { - $parts = self::parseUrl($matches[3], $dsn); + $parts = self::explodeUrl($matches[3], $dsn); return new Path($scheme, $parts['path'], self::getQuery($parts), $authentication); } - $parts = self::parseUrl('http://'.$matches[3], $dsn); + $parts = self::explodeUrl('http://'.$matches[3], $dsn); return new Url($scheme, $parts['host'], $parts['port'] ?? null, $parts['path'] ?? null, self::getQuery($parts), $authentication); } @@ -138,7 +159,7 @@ private static function getDsn(string $dsn): Dsn * * @throws SyntaxException */ - private static function parseUrl(string $url, string $dsn): array + private static function explodeUrl(string $url, string $dsn): array { $url = parse_url($url); if (false === $url) { diff --git a/tests/DsnParserTest.php b/tests/DsnParserTest.php index 989c09e..02308de 100644 --- a/tests/DsnParserTest.php +++ b/tests/DsnParserTest.php @@ -7,6 +7,7 @@ use Nyholm\Dsn\Configuration\Path; use Nyholm\Dsn\Configuration\Url; use Nyholm\Dsn\DsnParser; +use Nyholm\Dsn\Exception\DsnTypeNotSupported; use Nyholm\Dsn\Exception\FunctionsNotAllowedException; use Nyholm\Dsn\Exception\SyntaxException; use PHPUnit\Framework\TestCase; @@ -126,4 +127,28 @@ public function testParseInvalid(string $dsn) $this->expectException(SyntaxException::class); DsnParser::parseFunc($dsn); } + + public function testParseUrl() + { + $dsn = DsnParser::parseUrl('amqp://localhost'); + $this->assertInstanceOf(Url::class, $dsn); + } + + public function testParseUrlInvalid() + { + $this->expectException(DsnTypeNotSupported::class); + DsnParser::parseUrl('redis:///var/run/redis/redis.sock'); + } + + public function testParsePath() + { + $dsn = DsnParser::parsePath('redis:///var/run/redis/redis.sock'); + $this->assertInstanceOf(Path::class, $dsn); + } + + public function testParsePathInvalid() + { + $this->expectException(DsnTypeNotSupported::class); + DsnParser::parsePath('amqp://localhost'); + } }