Skip to content

Commit

Permalink
Refactor AbstractDsn class (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 14, 2024
1 parent 05d359c commit b9c2ce9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Enh #878: Realize `ColumnBuilder` class (@Tigrov)
- Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov)
- End #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov)
- Enh #885: Refactor `AbstractDsn` class (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
38 changes: 19 additions & 19 deletions src/Connection/AbstractDsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Stringable;

use function implode;

/**
* It's typically used to parse a DSN string, which is a string that has all the necessary information to connect
* to a database, such as the database driver, hostname, database name, port, and options.
Expand All @@ -17,37 +15,37 @@
abstract class AbstractDsn implements DsnInterface, Stringable
{
/**
* @psalm-param string[] $options
* @param string $driver The database driver name.
* @param string $host The database host name or IP address.
* @param string|null $databaseName The database name to connect to.
* @param string|null $port The database port. Null if isn't set.
* @param string[] $options The database connection options. Default value to an empty array.
*
* @psalm-param array<string,string> $options
*/
public function __construct(
private string $driver,
private string $host,
private string|null $databaseName = null,
private string|null $port = null,
private array $options = []
private readonly string $driver,
private readonly string $host,
private readonly string|null $databaseName = null,
private readonly string|null $port = null,
private readonly array $options = []
) {
}

public function asString(): string
{
$dsn = "$this->driver:" . "host=$this->host";
$dsn = "$this->driver:host=$this->host";

if ($this->databaseName !== null && $this->databaseName !== '') {
$dsn .= ';' . "dbname=$this->databaseName";
$dsn .= ";dbname=$this->databaseName";
}

if ($this->port !== null) {
$dsn .= ';' . "port=$this->port";
$dsn .= ";port=$this->port";
}

$parts = [];

foreach ($this->options as $key => $value) {
$parts[] = "$key=$value";
}

if (!empty($parts)) {
$dsn .= ';' . implode(';', $parts);
$dsn .= ";$key=$value";
}

return $dsn;
Expand Down Expand Up @@ -86,7 +84,9 @@ public function getHost(): string
}

/**
* @return array The database connection options. Default value to an empty array.
* @return string[] The database connection options. Default value to an empty array.
*
* @psalm-return array<string,string>
*/
public function getOptions(): array
{
Expand Down

0 comments on commit b9c2ce9

Please sign in to comment.