3
3
/**
4
4
* PHP DB Class
5
5
* 2021年01月07日 -> Class created
6
- * 2021年03月12日 -> Added rel="canoical |prev|next" to <a> pagination tags
6
+ * 2021年03月12日 -> Added rel="canonical |prev|next" to <a> pagination tags
7
7
* 2021年03月12日 -> Added a lot of comments
8
+ * 2021年06月05日 -> Added transformWith private method for insert and update methods using the $additional array. Added formatMonney public method to work with monetary values. Changed getPageNow to getCurrentPage
8
9
*
9
- * MUST FIX: delete(), $rules inside insert, define $functions on update and insert
10
10
*
11
11
* MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method
12
12
*/
@@ -54,7 +54,7 @@ class db
54
54
);
55
55
56
56
/** Will return a database instance. It also sets manny vars about the connection, if isn't yet defined */
57
- public static function getInstance ()
57
+ private static function getInstance ()
58
58
{
59
59
if (self ::$ dbInit == null ) {
60
60
self ::$ dbInit = microtime ();
@@ -220,7 +220,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
220
220
$ object ->extra ['limit ' ] = $ limit ;
221
221
self ::setPaginationWords ($ object , $ words );
222
222
if ($ page == false ) {
223
- $ page = self ::getPageNow ();
223
+ $ page = self ::getCurrentPage ();
224
224
}
225
225
$ newObject = self ::query ($ mixed . " LIMIT " . (($ limit * $ page ) - $ limit ) . ", " . $ limit );
226
226
return $ newObject ;
@@ -229,7 +229,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
229
229
$ totalRows = $ mixed ->extra ['totalEntries ' ];
230
230
$ object = new dbObject ($ mixed ->getInstance (), array ("limit " => $ limit , 'totalEntries ' => $ totalRows ));
231
231
self ::setPaginationWords ($ object , $ words );
232
- $ page = self ::getPageNow ();
232
+ $ page = self ::getCurrentPage ();
233
233
if ($ totalRows > $ limit ) {
234
234
$ newObject = self ::query ($ mixed ->getInstance ()->queryString . " LIMIT " . (($ limit * $ page ) - $ limit ) . ", " . $ limit );
235
235
return $ newObject ;
@@ -244,7 +244,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
244
244
self ::updateTotalRequests ();
245
245
$ object = new dbObject ($ mixed , array ("limit " => $ limit ));
246
246
self ::setPaginationWords ($ object , $ words );
247
- $ page = self ::getPageNow ();
247
+ $ page = self ::getCurrentPage ();
248
248
if ($ totalRows > $ limit ) {
249
249
$ newObject = self ::query ($ mixed ->queryString . " LIMIT " . (($ limit * $ page ) - $ limit ) . ", " . $ limit );
250
250
return $ newObject ;
@@ -283,7 +283,7 @@ public static function setPaginationWords($object, $words = false)
283
283
}
284
284
285
285
/** It retrieves the current page */
286
- private static function getPageNow ($ object = false )
286
+ private static function getCurrentPage ($ object = false )
287
287
{
288
288
if (!$ object ) {
289
289
$ object = self ::$ pageObject ;
@@ -327,7 +327,7 @@ public static function page($echo = true, $class = "")
327
327
}
328
328
$ words = self ::$ pageObject ->extra ['words ' ];
329
329
$ totalPages = ceil ($ totalRows / $ limit );
330
- $ pageNow = self ::getPageNow (self ::$ pageObject );
330
+ $ pageNow = self ::getCurrentPage (self ::$ pageObject );
331
331
if ($ class ) {
332
332
$ words ['class ' ] . "" . $ class . "" ;
333
333
}
@@ -530,19 +530,47 @@ public static function execute($sql)
530
530
return self ::getInstance ()->exec ($ sql );
531
531
}
532
532
533
- /** WORK IN PROGRESS - It will insert $data on a specific $table. The $rules are optional */
534
- public static function insert ($ data , $ table , $ rules = array ())
533
+ /** It will apply the additional steps before the real method applyment */
534
+ private static function transformWith (&$ data , $ additional )
535
+ {
536
+ if (isset ($ additional ['function ' ])) {
537
+ foreach ($ additional ['function ' ] as $ fieldKey => $ function ) {
538
+ if (isset ($ data [$ fieldKey ])) {
539
+ $ data [$ fieldKey ] = call_user_func ($ function , $ data [$ fieldKey ]);
540
+ }
541
+ }
542
+ }
543
+ }
544
+
545
+ /** This function is responsible to make the given value a decimal value for monetary operations */
546
+ /** It looks like the best option to save monney on a database is to use the DECIMAL 19,4 */
547
+ public static function formatMonney ($ value )
548
+ {
549
+ $ source = array ('. ' , ', ' );
550
+ $ replace = array ('' , '. ' );
551
+ $ value = str_replace ($ source , $ replace , $ value );
552
+ return $ value ;
553
+ }
554
+
555
+
556
+ /** It will insert $data on a specific $table. The $rules are optional */
557
+ public static function insert ($ data , $ table , $ additional = array ())
535
558
{
536
559
self ::fixDataCollumns ($ data , $ table , $ newData );
537
- $ sql = "INSERT INTO $ table ( " . implode (", " , array_keys ($ newData )) . ") VALUES (: " . implode (", : " , array_keys ($ newData )) . "); " ;
560
+ $ array_keys = array_keys ($ newData );
561
+ $ sql = "INSERT INTO $ table ( " . implode (", " , $ array_keys ) . ") VALUES (: " . implode (", : " , $ array_keys ) . "); " ;
538
562
$ object = self ::getInstance ();
539
563
$ stmnt = self ::prepare ($ object , $ sql );
564
+ if (!empty ($ additional )) {
565
+ self ::transformWith ($ newData , $ additional );
566
+ }
540
567
foreach ($ newData as $ key => $ value ) {
541
568
self ::set ($ stmnt , $ key , $ value );
542
569
}
543
570
if ($ stmnt ->execute ()) {
544
571
self ::updateTotalRequests ();
545
572
self ::$ id = $ object ->lastInsertId ();
573
+ unset($ stmnt , $ object );
546
574
return true ;
547
575
} else {
548
576
return false ;
@@ -556,7 +584,7 @@ public static function id()
556
584
}
557
585
558
586
/** It will update a row on a specific $table with new $data. The row must attend to the $rules */
559
- public static function update ($ data , $ table , $ rules = array ())
587
+ public static function update ($ data , $ table , $ rules = array (), $ additional = array () )
560
588
{
561
589
self ::fixDataCollumns ($ data , $ table , $ newData , "update " );
562
590
$ collumns = array ();
@@ -572,6 +600,9 @@ public static function update($data, $table, $rules = array())
572
600
$ sql = "UPDATE $ table SET " . implode (", " , $ collumns ) . (!empty ($ rules ) ? " WHERE " . implode (" AND " , $ newRules ) : "" ) . "; " ;
573
601
$ object = self ::getInstance ();
574
602
$ stmnt = self ::prepare ($ object , $ sql );
603
+ if (!empty ($ additional )) {
604
+ self ::transformWith ($ newData , $ additional );
605
+ }
575
606
foreach ($ newData as $ key => $ value ) {
576
607
self ::set ($ stmnt , $ key , $ value );
577
608
}
@@ -588,7 +619,7 @@ public static function update($data, $table, $rules = array())
588
619
}
589
620
}
590
621
591
- /** WORK IN PROGRESS - It will delete a record - or all - on a specific $table */
622
+ /** It will delete a record - or all - on a specific $table */
592
623
public static function delete ($ table , $ rules = array ())
593
624
{
594
625
if (!empty ($ rules )) {
0 commit comments