Initial
This commit is contained in:
120
sources/models/DBConfig.php
Normal file
120
sources/models/DBConfig.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace goodboyalex\php_db_components_pack\models;
|
||||
|
||||
use goodboyalex\php_components_pack\classes\Encryptor;
|
||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||
use goodboyalex\php_db_components_pack\enums\DBDriver;
|
||||
|
||||
/**
|
||||
* Модель параметров базы данных.
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
final class DBConfig implements ISerializable
|
||||
{
|
||||
/**
|
||||
* @var string $Host Хост базы данных.
|
||||
*/
|
||||
public string $Host;
|
||||
|
||||
/**
|
||||
* @var int $Port Порт базы данных.
|
||||
* @between 0...65535
|
||||
*/
|
||||
public int $Port;
|
||||
|
||||
/**
|
||||
* @var string $Name Имя базы данных.
|
||||
*/
|
||||
public string $Name;
|
||||
|
||||
/**
|
||||
* @var string $UserName Имя пользователя базы данных.
|
||||
*/
|
||||
public string $UserName;
|
||||
|
||||
/**
|
||||
* @var string $Password Пароль пользователя базы данных.
|
||||
*/
|
||||
public string $Password;
|
||||
|
||||
/**
|
||||
* @var DBDriver $Driver Тип драйвера базы данных.
|
||||
*/
|
||||
public DBDriver $Driver;
|
||||
|
||||
/**
|
||||
* @var string $Chipper Пароль шифрования.
|
||||
*/
|
||||
private string $Chipper;
|
||||
|
||||
/**
|
||||
* Конструктор.
|
||||
*
|
||||
* @param DBDriver $driver Тип драйвера базы данных.
|
||||
* @param string $host Хост базы данных.
|
||||
* @param int $port Порт базы данных.
|
||||
* @param string $name Имя базы данных.
|
||||
* @param string $userName Имя пользователя базы данных.
|
||||
* @param string $password Пароль пользователя базы данных.
|
||||
* @param string $chipper Пароль шифрования.
|
||||
*/
|
||||
public function __construct (DBDriver $driver = DBDriver::MySQL, string $host = "", int $port = 0,
|
||||
string $name = "", string $userName = "", string $password = "", string $chipper = "")
|
||||
{
|
||||
$this->Driver = $driver;
|
||||
$this->Host = $host;
|
||||
$this->Port = $port;
|
||||
$this->Name = $name;
|
||||
$this->UserName = $userName;
|
||||
$this->Password = $password;
|
||||
$this->Chipper = $chipper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function UnSerialize (string $serialized): void
|
||||
{
|
||||
// Десериализую массив
|
||||
$array = json_decode($serialized, true);
|
||||
|
||||
// Заполняю поля
|
||||
if (isset($array["host"]))
|
||||
$this->Host = $array["host"];
|
||||
if (isset($array["port"]))
|
||||
$this->Port = $array["port"];
|
||||
if (isset($array["name"]))
|
||||
$this->Name = $array["name"];
|
||||
if (isset($array["user"]))
|
||||
$this->UserName = $array["user"];
|
||||
if (isset($array["password"]))
|
||||
$this->Password = Encryptor::Decrypt($array["password"], $this->Chipper);
|
||||
if (isset($array["driver"]))
|
||||
$this->Driver = DBDriver::FromInt($array["driver"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function Serialize (): string
|
||||
{
|
||||
// Создаю массив результата
|
||||
$result = [];
|
||||
|
||||
// Заполняю массив
|
||||
$result["host"] = $this->Host;
|
||||
$result["port"] = $this->Port;
|
||||
$result["name"] = $this->Name;
|
||||
$result["user"] = $this->UserName;
|
||||
$result["password"] = Encryptor::Encrypt($this->Password, $this->Chipper);
|
||||
$result["driver"] = $this->Driver->ToInt();
|
||||
|
||||
// Сериализую
|
||||
return json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user