Advertisement
Guest User

Method Chain for Database (PDO)

a guest
Oct 17th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. class DatabaseInfo
  4. {
  5.     /**
  6.      * データベースの種類
  7.      * @var string
  8.      */
  9.     private $database_type;
  10.  
  11.     /**
  12.      * データベースサーバー名
  13.      * @var string
  14.      */
  15.     private $database_server;
  16.  
  17.     /**
  18.      * データベースのポート
  19.      * @var string|int
  20.      */
  21.     private $database_port;
  22.  
  23.  
  24.     /**
  25.      * データベースのユーザー名
  26.      * @var string
  27.      */
  28.     private $database_username;
  29.  
  30.     /**
  31.      * データベースのパスワード
  32.      * @var string
  33.      */
  34.     private $database_password;
  35.  
  36.     /**
  37.      * データベース名
  38.      * @var string
  39.      */
  40.     private $database_name;
  41.  
  42.     /**
  43.      * データベースの文字コード
  44.      * @var string
  45.      */
  46.     private $database_charset;
  47.  
  48.     public function type($dbtype = null)
  49.     {
  50.         $this->database_type = $dbtype;
  51.         return $this;
  52.     }
  53.     public function port($port) {
  54.         $this->database_port = $port;
  55.         return $this;
  56.     }
  57.  
  58.     public function dbname($dbname = null)
  59.     {
  60.         $this->database_name = $dbname;
  61.         return $this;
  62.     }
  63.  
  64.     public function charset($charset = null)
  65.     {
  66.         $this->database_charset = $charset;
  67.         return $this;
  68.     }
  69.  
  70.     public function server($server = null)
  71.     {
  72.         $this->database_server = $server;
  73.         return $this;
  74.     }
  75.  
  76.     public function username($username = null)
  77.     {
  78.         $this->database_username = $username;
  79.         return $this;
  80.     }
  81.  
  82.     public function password($password = null)
  83.     {
  84.         $this->database_password = $password;
  85.         return $this;
  86.     }
  87.  
  88.     public function getDdn()
  89.     {
  90.         if(!isset($this->database_port)){
  91.             return "{$this->database_type}:host={$this->database_server};dbname={$this->database_name};charset={$this->database_charset}";
  92.         }else{
  93.             return "{$this->database_type}:host={$this->database_server};port={$this->database_port};dbname={$this->database_name};charset={$this->database_charset}";
  94.         }
  95.     }
  96.  
  97.     public function getUserName()
  98.     {
  99.         return $this->database_username;
  100.     }
  101.  
  102.     public function getPassword()
  103.     {
  104.         return $this->database_password;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement