Merge pull request #3378 from appwrite/feat-add-dsn-class
feat: add DSN
This commit is contained in:
commit
74cd77e2f1
135
src/Appwrite/DSN/DSN.php
Normal file
135
src/Appwrite/DSN/DSN.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\DSN;
|
||||
|
||||
class DSN
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $scheme;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $user;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $password;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $host;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $port;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $database;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $query;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* Construct a new DSN object
|
||||
*
|
||||
* @param string $dsn
|
||||
*/
|
||||
public function __construct(string $dsn)
|
||||
{
|
||||
$parts = parse_url($dsn);
|
||||
|
||||
if (!$parts) {
|
||||
throw new \InvalidArgumentException("Unable to parse DSN: $dsn");
|
||||
}
|
||||
|
||||
$this->scheme = $parts['scheme'] ?? null;
|
||||
$this->user = $parts['user'] ?? null;
|
||||
$this->password = $parts['pass'] ?? null;
|
||||
$this->host = $parts['host'] ?? null;
|
||||
$this->port = $parts['port'] ?? null;
|
||||
$this->database = $parts['path'] ?? null;
|
||||
$this->query = $parts['query'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the scheme.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getScheme(): string
|
||||
{
|
||||
return $this->scheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user.
|
||||
*
|
||||
* @return ?string
|
||||
*/
|
||||
public function getUser(): ?string
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the password.
|
||||
*
|
||||
* @return ?string
|
||||
*/
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the host
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the port
|
||||
*
|
||||
* @return ?string
|
||||
*/
|
||||
public function getPort(): ?string
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the database
|
||||
*
|
||||
* @return ?string
|
||||
*/
|
||||
public function getDatabase(): ?string
|
||||
{
|
||||
return ltrim($this->database, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the query string
|
||||
*
|
||||
* @return ?string
|
||||
*/
|
||||
public function getQuery(): ?string
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
}
|
89
tests/unit/DSN/DSNTest.php
Normal file
89
tests/unit/DSN/DSNTest.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Tests;
|
||||
|
||||
use Appwrite\DSN\DSN;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DSNTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testSuccess(): void
|
||||
{
|
||||
$dsn = new DSN("mariadb://user:password@localhost:3306/database?charset=utf8&timezone=UTC");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertEquals("password", $dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertEquals("3306", $dsn->getPort());
|
||||
$this->assertEquals("database", $dsn->getDatabase());
|
||||
$this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://user@localhost:3306/database?charset=utf8&timezone=UTC");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertNull($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertEquals("3306", $dsn->getPort());
|
||||
$this->assertEquals("database", $dsn->getDatabase());
|
||||
$this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://user@localhost/database?charset=utf8&timezone=UTC");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertNull($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertNull($dsn->getPort());
|
||||
$this->assertEquals("database", $dsn->getDatabase());
|
||||
$this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://user@localhost?charset=utf8&timezone=UTC");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertNull($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertNull($dsn->getPort());
|
||||
$this->assertEmpty($dsn->getDatabase());
|
||||
$this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://user@localhost");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertNull($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertNull($dsn->getPort());
|
||||
$this->assertEmpty($dsn->getDatabase());
|
||||
$this->assertNull($dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://user:@localhost");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertEquals("user", $dsn->getUser());
|
||||
$this->assertEmpty($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertNull($dsn->getPort());
|
||||
$this->assertEmpty($dsn->getDatabase());
|
||||
$this->assertNull($dsn->getQuery());
|
||||
|
||||
$dsn = new DSN("mariadb://localhost");
|
||||
$this->assertEquals("mariadb", $dsn->getScheme());
|
||||
$this->assertNull($dsn->getUser());
|
||||
$this->assertNull($dsn->getPassword());
|
||||
$this->assertEquals("localhost", $dsn->getHost());
|
||||
$this->assertNull($dsn->getPort());
|
||||
$this->assertEmpty($dsn->getDatabase());
|
||||
$this->assertNull($dsn->getQuery());
|
||||
}
|
||||
|
||||
public function testFail(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$dsn = new DSN("mariadb://");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue