@@ -21,105 +21,105 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class MySQL implements ConnectableInterface |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var string $dsn |
|
| 26 | - */ |
|
| 27 | - protected $dsn; |
|
| 24 | + /** |
|
| 25 | + * @var string $dsn |
|
| 26 | + */ |
|
| 27 | + protected $dsn; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var \PDO $connectionObject |
|
| 31 | - */ |
|
| 32 | - protected $connectionObject; |
|
| 29 | + /** |
|
| 30 | + * @var \PDO $connectionObject |
|
| 31 | + */ |
|
| 32 | + protected $connectionObject; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var array loginData |
|
| 36 | - */ |
|
| 37 | - private $loginData = []; |
|
| 34 | + /** |
|
| 35 | + * @var array loginData |
|
| 36 | + */ |
|
| 37 | + private $loginData = []; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Setter for the DSN string {@see $dsn} |
|
| 41 | - * |
|
| 42 | - * @param array $dsnArray |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function setDsn(array $dsnArray) |
|
| 46 | - { |
|
| 47 | - $this->dsn = $dsnArray; |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * Setter for the DSN string {@see $dsn} |
|
| 41 | + * |
|
| 42 | + * @param array $dsnArray |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function setDsn(array $dsnArray) |
|
| 46 | + { |
|
| 47 | + $this->dsn = $dsnArray; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Establishes connection |
|
| 52 | - * |
|
| 53 | - * @param string $username |
|
| 54 | - * @param string $password optional |
|
| 55 | - * @throws \EmmetBlue\Core\Exception\SQLException |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function connect(string $username, string $password="") |
|
| 59 | - { |
|
| 60 | - $host = $this->dsn[0]; |
|
| 61 | - $database = $this->dsn[1]; |
|
| 50 | + /** |
|
| 51 | + * Establishes connection |
|
| 52 | + * |
|
| 53 | + * @param string $username |
|
| 54 | + * @param string $password optional |
|
| 55 | + * @throws \EmmetBlue\Core\Exception\SQLException |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function connect(string $username, string $password="") |
|
| 59 | + { |
|
| 60 | + $host = $this->dsn[0]; |
|
| 61 | + $database = $this->dsn[1]; |
|
| 62 | 62 | |
| 63 | - $this->loginData['username'] = $username; |
|
| 64 | - $this->loginData['password'] = $password; |
|
| 63 | + $this->loginData['username'] = $username; |
|
| 64 | + $this->loginData['password'] = $password; |
|
| 65 | 65 | |
| 66 | - try |
|
| 67 | - { |
|
| 68 | - $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
|
| 69 | - } |
|
| 70 | - catch (\PDOException $e) |
|
| 71 | - { |
|
| 72 | - throw new SQLException("Unable to connect to database", 400, $e); |
|
| 73 | - } |
|
| 66 | + try |
|
| 67 | + { |
|
| 68 | + $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
|
| 69 | + } |
|
| 70 | + catch (\PDOException $e) |
|
| 71 | + { |
|
| 72 | + throw new SQLException("Unable to connect to database", 400, $e); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $dsn = $this->dsn; |
|
| 76 | - unset($dsn[0], $dsn[1]); |
|
| 75 | + $dsn = $this->dsn; |
|
| 76 | + unset($dsn[0], $dsn[1]); |
|
| 77 | 77 | |
| 78 | - foreach ($dsn as $attribute) |
|
| 79 | - { |
|
| 80 | - foreach ($attribute as $key=>$value) |
|
| 81 | - { |
|
| 82 | - $this->connectionObject->setAttribute($key, $value); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } |
|
| 78 | + foreach ($dsn as $attribute) |
|
| 79 | + { |
|
| 80 | + foreach ($attribute as $key=>$value) |
|
| 81 | + { |
|
| 82 | + $this->connectionObject->setAttribute($key, $value); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Returns an instance a pdo intance of the connection object |
|
| 89 | - * |
|
| 90 | - * @return \PDO |
|
| 91 | - */ |
|
| 92 | - public function getConnection() : \PDO |
|
| 93 | - { |
|
| 94 | - if ($this->connectionObject instanceof \PDO) |
|
| 95 | - { |
|
| 96 | - return $this->connectionObject; |
|
| 97 | - } |
|
| 87 | + /** |
|
| 88 | + * Returns an instance a pdo intance of the connection object |
|
| 89 | + * |
|
| 90 | + * @return \PDO |
|
| 91 | + */ |
|
| 92 | + public function getConnection() : \PDO |
|
| 93 | + { |
|
| 94 | + if ($this->connectionObject instanceof \PDO) |
|
| 95 | + { |
|
| 96 | + return $this->connectionObject; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - $this->connect( |
|
| 100 | - $this->loginData['username'] ?? '', |
|
| 101 | - $this->loginData['password'] ?? '' |
|
| 102 | - ); |
|
| 99 | + $this->connect( |
|
| 100 | + $this->loginData['username'] ?? '', |
|
| 101 | + $this->loginData['password'] ?? '' |
|
| 102 | + ); |
|
| 103 | 103 | |
| 104 | - return $this->connectionObject; |
|
| 105 | - } |
|
| 104 | + return $this->connectionObject; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Closes connection |
|
| 109 | - */ |
|
| 110 | - public function disableConnection() |
|
| 111 | - { |
|
| 112 | - $this->connectionObject = null; |
|
| 113 | - } |
|
| 107 | + /** |
|
| 108 | + * Closes connection |
|
| 109 | + */ |
|
| 110 | + public function disableConnection() |
|
| 111 | + { |
|
| 112 | + $this->connectionObject = null; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Sets attributes for the PDO object |
|
| 117 | - * |
|
| 118 | - * @param \PDO $attribute |
|
| 119 | - * @param \PDO $value |
|
| 120 | - */ |
|
| 121 | - public function setAttribute(\PDO $attribute, \PDO $value) |
|
| 122 | - { |
|
| 123 | - $this->connectionObject->setAttribute($attribute, $value); |
|
| 124 | - } |
|
| 115 | + /** |
|
| 116 | + * Sets attributes for the PDO object |
|
| 117 | + * |
|
| 118 | + * @param \PDO $attribute |
|
| 119 | + * @param \PDO $value |
|
| 120 | + */ |
|
| 121 | + public function setAttribute(\PDO $attribute, \PDO $value) |
|
| 122 | + { |
|
| 123 | + $this->connectionObject->setAttribute($attribute, $value); |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -66,8 +66,7 @@ |
||
| 66 | 66 | try |
| 67 | 67 | { |
| 68 | 68 | $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
| 69 | - } |
|
| 70 | - catch (\PDOException $e) |
|
| 69 | + } catch (\PDOException $e) |
|
| 71 | 70 | { |
| 72 | 71 | throw new SQLException("Unable to connect to database", 400, $e); |
| 73 | 72 | } |