Skip to content

Commit

Permalink
Added parseUrl() and parsePath() (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored Oct 11, 2020
1 parent 6bd5d51 commit 1d75b45
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions src/DsnParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions tests/DsnParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 1d75b45

Please sign in to comment.