Created
October 21, 2017 14:08
-
-
Save anonymous/99cc8d72f49433b301ad5216ccd0566e to your computer and use it in GitHub Desktop.
Database Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Config | |
{ | |
/** @var array */ | |
private $_config; | |
/** | |
* データベースの種類 | |
* @param $type | |
* @return $this | |
*/ | |
public function type($type) | |
{ | |
$this->_config['type'] = $this->detectDatabaseType($type); | |
return $this; | |
} | |
/** | |
* データベースの種類を検出する | |
* @param $dbname | |
* @return mixed | |
*/ | |
private function detectDatabaseType($dbname) | |
{ | |
// データベースの種類 | |
// とりあえずMySQL/PostgreSQL/SQLiteのみ | |
$databaseArray = array( | |
'MySQL' => 'mysql', | |
'PostgreSQL' => 'pgsql', | |
'SQLite' => 'sqlite', | |
); | |
return $databaseArray[$dbname]; | |
} | |
/** | |
* データベースサーバー | |
* @param $server | |
* @return $this | |
*/ | |
public function server($server) | |
{ | |
$this->_config['server'] = $server; | |
return $this; | |
} | |
/** | |
* データベースのポート | |
* @param $port | |
* @return $this | |
*/ | |
public function port($port) | |
{ | |
$this->_config['port'] = $port; | |
return $this; | |
} | |
/** | |
* データベースのユーザー名 | |
* @param $username | |
* @return $this | |
*/ | |
public function username($username) | |
{ | |
$this->_config['username'] = $username; | |
return $this; | |
} | |
/** | |
* データベースのパスワード | |
* @param $password | |
* @return $this | |
*/ | |
public function password($password) | |
{ | |
$this->_config['password'] = $password; | |
return $this; | |
} | |
/** | |
* データベース名 | |
* @param $dbname | |
* @return $this | |
*/ | |
public function dbname($dbname) | |
{ | |
$this->_config['dbname'] = $dbname; | |
return $this; | |
} | |
/** | |
* 文字コード | |
* @param $charset | |
* @return $this | |
*/ | |
public function charset($charset) | |
{ | |
$this->_config['charset'] = $this->detectCharset($charset); | |
return $this; | |
} | |
/** | |
* 文字コードを検出する | |
* @param $charset | |
* @return mixed | |
*/ | |
public function detectCharset($charset) | |
{ | |
$charsetArray = array( | |
'UTF-8' => 'utf8', | |
'Shift_JIS' => 'sjis', | |
'EUC-JP' => 'ujis', | |
); | |
return $charsetArray[$charset]; | |
} | |
/** | |
* データソース名を返す | |
* @return string | |
*/ | |
public function buildDSN() | |
{ | |
if ($this->_config['type'] === 'mysql') { | |
if (!isset($this->_config['port'])) { | |
return "mysql:host={$this->_config['server']};dbname={$this->_config['dbname']};charset={$this->_config['charset']}"; | |
} else { | |
return "mysql:host={$this->_config['server']};port={$this->_config['port']};dbname={$this->_config['dbname']};charset={$this->_config['charset']}"; | |
} | |
} elseif ($this->_config['type'] === 'pgsql') { | |
if (!isset($this->_config['port'])) { | |
return "pgsql:host={$this->_config['server']};dbname={$this->_config['dbname']};charset={$this->_config['charset']}"; | |
} else { | |
return "pgsql:host={$this->_config['server']};port={$this->_config['port']};dbname={$this->_config['dbname']};charset={$this->_config['charset']}"; | |
} | |
} elseif ($this->_config['type'] === 'sqlite') { | |
return "sqlite:{$this->_config['dbname']}"; | |
} | |
} | |
/** | |
* ユーザー名を返す | |
* @return string | |
*/ | |
public function buildUserName() | |
{ | |
return "{$this->_config['server']}"; | |
} | |
/** | |
* パスワードを返す | |
* @return string | |
*/ | |
public function buildPassword() | |
{ | |
return "{$this->_config['password']}"; | |
} | |
/** | |
* @return static | |
*/ | |
public static function forge() | |
{ | |
return new static; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment