63 lines
1.5 KiB
PHP
Raw Normal View History

2024-08-29 21:57:53 +03:00
<?php
namespace ContingentParser\Database;
final class DatabaseConfig
{
2024-09-03 20:16:34 +03:00
private string $driver;
private string $host;
private string $dbname;
private string $port;
private string $charset;
private string $username;
private string $password;
2024-08-29 21:57:53 +03:00
public function __construct(string $db)
{
$config = $this->getDataEnv($db);
2024-09-03 20:16:34 +03:00
$this->driver = $config['DB_DRIVER'];
$this->host = $config['DB_HOST'];
$this->dbname = $config['DB_NAME'];
$this->port = $config['DB_PORT'];
$this->charset = $config["DB_CHARSET"];
$this->username = $config['DB_USERNAME'];
$this->password = $config['DB_PASSWORD'];
2024-08-29 21:57:53 +03:00
}
private function getDataEnv(string $db) : array
{
$envVars = parse_ini_file('.env', true);
$db = strtoupper($db);
$config = [];
foreach ($envVars as $dbname => $dbconfig) {
if ($dbname == $db) {
$config = $dbconfig;
}
}
return $config;
}
public function getDBName(): string
{
2024-09-03 20:16:34 +03:00
return $this->dbname;
2024-08-29 21:57:53 +03:00
}
public function getDsn() : string
{
2024-09-03 20:16:34 +03:00
return $this->driver.":host=".$this->host
.";dbname=".$this->dbname
.";charset=".$this->charset
.";port=".$this->port;
2024-08-29 21:57:53 +03:00
}
public function getUsername() : string
{
2024-09-03 20:16:34 +03:00
return $this->username;
2024-08-29 21:57:53 +03:00
}
public function getPassword() : string
{
2024-09-03 20:16:34 +03:00
return $this->password;
2024-08-29 21:57:53 +03:00
}
2024-09-03 20:16:34 +03:00
}