33 * @author : Jakiboy
44 * @package : FloatPHP
55 * @subpackage : Classes Connection Component
6- * @version : 1.2 .x
6+ * @version : 1.3 .x
77 * @copyright : (c) 2018 - 2024 Jihad Sinnaour <[email protected] > 88 * @link : https://floatphp.com
99 * @license : MIT
1616namespace FloatPHP \Classes \Connection ;
1717
1818use FloatPHP \Interfaces \Classes \LoggerInterface ;
19- use FloatPHP \Classes \Filesystem \{
20- TypeCheck , Stringify , Arrayify
21- };
19+ use FloatPHP \Classes \Filesystem \{TypeCheck , Stringify , Arrayify };
2220use \PDOException ;
2321use \PDO ;
2422
@@ -41,23 +39,24 @@ class Db
4139 /**
4240 * Connect to database.
4341 *
42+ * @access public
4443 * @param array $config
4544 * @param object LoggerInterface $logger
4645 */
47- public function __construct (array $ config = [], LoggerInterface $ logger = null )
46+ public function __construct (array $ config = [], ? LoggerInterface $ logger = null )
4847 {
4948 $ this ->logger = $ logger ;
5049 $ this ->connect ($ config );
5150 $ this ->parameters = [];
5251 }
53-
52+
5453 /**
5554 * Close connection.
5655 *
5756 * @access public
5857 * @return void
5958 */
60- public function close ()
59+ public function close () : void
6160 {
6261 $ this ->pdo = null ;
6362 }
@@ -70,7 +69,7 @@ public function close()
7069 * @param mixed $value
7170 * @return void
7271 */
73- public function bind (string $ bind , $ value = null )
72+ public function bind (string $ bind , $ value = null ) : void
7473 {
7574 $ this ->parameters [sizeof ($ this ->parameters )] = [": {$ bind }" , $ value ];
7675 }
@@ -82,7 +81,7 @@ public function bind(string $bind, $value = null)
8281 * @param array $bind
8382 * @return void
8483 */
85- public function bindMore (array $ bind )
84+ public function bindMore (array $ bind ) : void
8685 {
8786 if ( empty ($ this ->parameters ) ) {
8887 if ( TypeCheck::isArray ($ bind ) ) {
@@ -94,28 +93,32 @@ public function bindMore(array $bind)
9493 }
9594 }
9695
97- /**
98- * Get query result.
99- *
96+ /**
97+ * Get query result.
98+ *
99+ * [FETCH_ASSOC : 2].
100+ *
100101 * @access public
101102 * @param string $sql
102103 * @param array $params
103104 * @param int $mode
104105 * @return mixed
105106 */
106- public function query (string $ sql , ?array $ params = null , int $ mode = PDO :: FETCH_ASSOC )
107+ public function query (string $ sql , ?array $ params = null , int $ mode = 2 ) : mixed
107108 {
108109 // Format SQL
109- $ sql = trim (Stringify::replace ("\r" , ' ' , $ sql ));
110+ $ sql = Stringify::replace (search: "\r" , replace: ' ' , subject: $ sql );
111+ $ sql = trim ($ sql );
110112
111113 // Init SQL
112114 $ this ->init ($ sql , $ params );
113115
114116 // Catch SQL statement
115117 if ( $ this ->getStatementType ($ sql ) == 'read ' ) {
116118 return $ this ->query ->fetchAll ($ mode );
119+ }
117120
118- } elseif ( $ this ->getStatementType ($ sql ) == 'write ' ) {
121+ if ( $ this ->getStatementType ($ sql ) == 'write ' ) {
119122 return $ this ->query ->rowCount ();
120123 }
121124
@@ -128,11 +131,11 @@ public function query(string $sql, ?array $params = null, int $mode = PDO::FETCH
128131 * @access public
129132 * @return mixed
130133 */
131- public function lastInsertId ()
134+ public function lastInsertId () : mixed
132135 {
133136 return $ this ->pdo ->lastInsertId ();
134137 }
135-
138+
136139 /**
137140 * Begin transaction.
138141 *
@@ -143,7 +146,7 @@ public function beginTransaction() : bool
143146 {
144147 return $ this ->pdo ->beginTransaction ();
145148 }
146-
149+
147150 /**
148151 * Execute transaction.
149152 *
@@ -154,7 +157,7 @@ public function executeTransaction() : bool
154157 {
155158 return $ this ->pdo ->commit ();
156159 }
157-
160+
158161 /**
159162 * Rollback transaction.
160163 *
@@ -165,7 +168,7 @@ public function rollBack() : bool
165168 {
166169 return $ this ->pdo ->rollBack ();
167170 }
168-
171+
169172 /**
170173 * Returns column from the result set.
171174 *
@@ -174,7 +177,7 @@ public function rollBack() : bool
174177 * @param array $params
175178 * @return mixed
176179 */
177- public function column (string $ sql , ?array $ params = null )
180+ public function column (string $ sql , ?array $ params = null ) : mixed
178181 {
179182 $ this ->init ($ sql , $ params );
180183 $ columns = $ this ->query ->fetchAll (PDO ::FETCH_NUM );
@@ -188,33 +191,35 @@ public function column(string $sql, ?array $params = null)
188191 /**
189192 * Returns row from the result set.
190193 *
194+ * [FETCH_ASSOC : 2].
195+ *
191196 * @access public
192197 * @param string $sql
193198 * @param array $params
194199 * @param int $mode
195200 * @return mixed
196201 */
197- public function row (string $ sql , ?array $ params = null , int $ mode = PDO :: FETCH_ASSOC )
202+ public function row (string $ sql , ?array $ params = null , int $ mode = 2 ) : mixed
198203 {
199204 $ this ->init ($ sql , $ params );
200205 $ result = $ this ->query ->fetch ($ mode );
201- $ this ->query ->closeCursor ();
206+ $ this ->query ->closeCursor ();
202207 return $ result ;
203208 }
204209
205210 /**
206- * Returns value of one single field (column).
211+ * Returns value of one single field (column).
207212 *
208213 * @access public
209214 * @param string $sql
210215 * @param array $params
211216 * @return mixed
212217 */
213- public function single (string $ sql , ?array $ params = null )
218+ public function single (string $ sql , ?array $ params = null ) : mixed
214219 {
215220 $ this ->init ($ sql , $ params );
216221 $ result = $ this ->query ->fetchColumn ();
217- $ this ->query ->closeCursor ();
222+ $ this ->query ->closeCursor ();
218223 return $ result ;
219224 }
220225
@@ -225,24 +230,24 @@ public function single(string $sql, ?array $params = null)
225230 * @param array $config
226231 * @return void
227232 */
228- protected function connect (array $ config = [])
233+ protected function connect (array $ config = []) : void
229234 {
230235 try {
231236
232237 // Get configuration
233238 $ config = Arrayify::merge ([
234- 'db ' => '' ,
239+ 'name ' => '' ,
235240 'host ' => 'localhost ' ,
236241 'port ' => 3306 ,
237- 'user ' => '' ,
242+ 'user ' => '' ,
238243 'pswd ' => '' ,
239244 'charset ' => 'utf8 '
240245 ], $ config );
241246
242247 extract ($ config );
243248
244- $ dsn = "mysql:dbname= {$ db };host= {$ host };port= {$ port }" ;
245- $ this ->pdo = new PDO ($ dsn , $ user , $ pswd , [
249+ $ dsn = "mysql:dbname= {$ name };host= {$ host };port= {$ port }" ;
250+ $ this ->pdo = new PDO ($ dsn , $ user , $ pswd , options: [
246251 PDO ::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$ charset }"
247252 ]);
248253
@@ -269,7 +274,7 @@ protected function connect(array $config = [])
269274 * @param array $params
270275 * @return void
271276 */
272- protected function init (string $ sql , array $ params = [])
277+ protected function init (string $ sql , array $ params = []) : void
273278 {
274279 // Connect to database
275280 if ( !$ this ->isConnected ) {
@@ -324,17 +329,18 @@ protected function init(string $sql, array $params = [])
324329 * @param string $sql
325330 * @return mixed
326331 */
327- protected function getStatementType (string $ sql = '' )
332+ protected function getStatementType (string $ sql = '' ) : mixed
328333 {
329- $ sql = Stringify::replaceRegex ("/\s+| \t+| \n+/ " , ' ' , $ sql );
334+ $ sql = Stringify::replaceRegex (regex: "/\s+| \t+| \n+/ " , replace: ' ' , subject: $ sql );
330335 $ raw = explode (' ' , $ sql );
331336 $ header = $ raw [0 ] ?? '' ;
332337 $ st = strtolower ($ header );
333338
334339 if ( $ st == 'select ' || $ st == 'show ' ) {
335340 return 'read ' ;
341+ }
336342
337- } elseif ( $ st == 'insert ' || $ st == 'update ' || $ st == 'delete ' ) {
343+ if ( $ st == 'insert ' || $ st == 'update ' || $ st == 'delete ' ) {
338344 return 'write ' ;
339345 }
340346
@@ -349,17 +355,20 @@ protected function getStatementType(string $sql = '')
349355 * @param string $sql
350356 * @return string
351357 */
352- protected function log (?string $ message = null , string $ sql = null )
358+ protected function log (?string $ message = null , string $ sql = null ) : string
353359 {
354360 if ( empty ($ message ) ) {
355361 $ message = 'Unhandled Exception ' ;
356362 }
363+
357364 if ( !empty ($ sql ) ) {
358365 $ message .= "\r\nRaw SQL : {$ sql }" ;
359366 }
367+
360368 if ( $ this ->logger ) {
361369 $ this ->logger ->error ($ message );
362370 }
363- echo $ message ;
371+
372+ return $ message ;
364373 }
365374}
0 commit comments