2
2
3
3
/**
4
4
* PHP DB Class
5
+ * 2021年01月07日 -> Class created
6
+ * 2021年03月12日 -> Added rel="canoical|prev|next" to <a> pagination tags
7
+ * 2021年03月12日 -> Added a lot of comments
8
+ *
9
+ * MUST FIX: delete(), $rules inside insert, define $functions on update and insert
10
+ *
11
+ * MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method
5
12
*/
6
13
7
14
class db
8
15
{
16
+ /** This stores the current $connectionName */
9
17
protected static $ connectionName = null ;
18
+
19
+ /** This array stores all $connections added so far on execution */
10
20
protected static $ connections = array ();
21
+
22
+ /** This will receive the last inserted row $id */
11
23
protected static $ id = null ;
24
+
25
+ /** This is an array with the objects keys. */
12
26
protected static $ object = array ();
27
+
28
+ /** It is the $pagedObject. It is set by pagedQuery method */
13
29
protected static $ pageObject = null ;
30
+
31
+ /** It will receive a $friendURL instance to work with friendly URLs */
14
32
protected static $ friendlyURL = false ;
33
+
34
+ /** It will receive the time() when the class has first executed anything */
15
35
private static $ dbInit = null ;
36
+
37
+ /** This is the default class $language */
16
38
protected static $ language = "en " ;
39
+
40
+ /** These are the words that pagination may have */
17
41
protected static $ defaultPaginationWords = array (
18
42
"en " => array (
19
43
"class " => "pagination " ,
@@ -29,6 +53,7 @@ class db
29
53
)
30
54
);
31
55
56
+ /** Will return a database instance. It also sets manny vars about the connection, if isn't yet defined */
32
57
public static function getInstance ()
33
58
{
34
59
if (self ::$ dbInit == null ) {
@@ -62,6 +87,7 @@ public static function getInstance()
62
87
}
63
88
}
64
89
90
+ /** Adds a database connection */
65
91
public static function addConnection ($ connectionName , $ connectionCredentials )
66
92
{
67
93
if (!array_key_exists ($ connectionName , self ::$ connections )) {
@@ -70,6 +96,7 @@ public static function addConnection($connectionName, $connectionCredentials)
70
96
}
71
97
}
72
98
99
+ /** Defines which connection the class will use */
73
100
public static function useConnection ($ connectionName )
74
101
{
75
102
if (array_key_exists ($ connectionName , self ::$ connections )) {
@@ -78,6 +105,7 @@ public static function useConnection($connectionName)
78
105
}
79
106
}
80
107
108
+ /** Sets total amount of requests to +1 */
81
109
private static function updateTotalRequests ()
82
110
{
83
111
if (!isset (self ::$ connections [self ::$ connectionName ]['totalRequests ' ])) {
@@ -86,16 +114,19 @@ private static function updateTotalRequests()
86
114
self ::$ connections [self ::$ connectionName ]['totalRequests ' ]++;
87
115
}
88
116
117
+ /** Returns the total amount of requests that the class did so far */
89
118
public static function getTotalRequests ()
90
119
{
91
120
return (isset (self ::$ connections [self ::$ connectionName ]['totalRequests ' ]) ? self ::$ connections [self ::$ connectionName ]['totalRequests ' ] : 0 );
92
121
}
93
122
123
+ /** Checks the server speed */
94
124
public static function performance ()
95
125
{
96
126
echo "<!-- Time ellapsed: " . ((int ) microtime () - (int ) self ::$ dbInit ) . " --> " ;
97
127
}
98
128
129
+ /** Creates a new DB Object with the given PDO instance */
99
130
private static function encapsulate ($ mixed )
100
131
{
101
132
if (is_string ($ mixed )) {
@@ -116,6 +147,7 @@ private static function encapsulate($mixed)
116
147
}
117
148
}
118
149
150
+ /** Returns a single row */
119
151
public static function fetch ($ mixed , $ simple = false )
120
152
{
121
153
if (is_string ($ mixed )) {
@@ -139,6 +171,7 @@ public static function fetch($mixed, $simple = false)
139
171
}
140
172
}
141
173
174
+ /** Returns all fetched data */
142
175
public static function fetchAll ($ mixed )
143
176
{
144
177
$ mixed = self ::encapsulate ($ mixed );
@@ -154,12 +187,14 @@ public static function fetchAll($mixed)
154
187
}
155
188
}
156
189
190
+ /** It counts the total rows that $mixed have */
157
191
public static function count ($ mixed )
158
192
{
159
193
$ object = self ::encapsulate ($ mixed );
160
194
return $ object ->extra ['totalEntries ' ];
161
195
}
162
196
197
+ /** Checks if the given $query returns null. If it returns null or 0, the function return true (is empty) */
163
198
public static function empty ($ query )
164
199
{
165
200
if (is_string ($ query )) {
@@ -171,11 +206,13 @@ public static function empty($query)
171
206
}
172
207
}
173
208
209
+ /** Simple query */
174
210
public static function query ($ mixed )
175
211
{
176
212
return self ::encapsulate ($ mixed );
177
213
}
178
214
215
+ /** This is a paged query */
179
216
public static function pagedQuery ($ mixed , $ limit , $ page = false , $ words = false )
180
217
{
181
218
if (is_string ($ mixed )) {
@@ -218,12 +255,14 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
218
255
}
219
256
}
220
257
258
+ /** It sets the class $language */
221
259
public static function setLanguage ($ language )
222
260
{
223
261
self ::$ language = $ language ;
224
262
return new static ();
225
263
}
226
264
265
+ /** It defines the words that the pagination HTML will have */
227
266
public static function setPaginationWords ($ object , $ words = false )
228
267
{
229
268
try {
@@ -243,6 +282,7 @@ public static function setPaginationWords($object, $words = false)
243
282
}
244
283
}
245
284
285
+ /** It retrieves the current page */
246
286
private static function getPageNow ($ object = false )
247
287
{
248
288
if (!$ object ) {
@@ -274,6 +314,7 @@ private static function getPageNow($object = false)
274
314
return $ pageNow ;
275
315
}
276
316
317
+ /** It creates the pagination HTML */
277
318
public static function page ($ echo = true , $ class = "" )
278
319
{
279
320
$ totalRows = self ::$ pageObject ->extra ['totalEntries ' ];
@@ -307,15 +348,15 @@ public static function page($echo = true, $class = "")
307
348
}
308
349
}
309
350
$ htmlWrapper = "<ul class='pagination {additional}'>{buttons}</ul> " ;
310
- $ htmlButton = "<li {disabled}><a . $ words ['class ' ] . " {active}' href='{target}'>{text|number}</a></li> " ;
351
+ $ htmlButton = "<li {disabled}><a {rel} class=' " . $ words ['class ' ] . " {active}' href='{target}'>{text|number}</a></li> " ;
311
352
$ buttons = array ();
312
353
if ($ pageNow > 1 ) {
313
354
if (self ::$ friendlyURL ) {
314
355
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageNow - 1 );
315
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "/ " . implode ("/ " , $ parts ), $ words ["prev " ], '' , '' ), $ htmlButton );
356
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array (" rel='prev' " , $ url . "/ " . implode ("/ " , $ parts ), $ words ["prev " ], '' , '' ), $ htmlButton );
316
357
} else {
317
358
$ _GET [$ words ['url ' ]] = $ pageNow - 1 ;
318
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "? " . http_build_query ($ _GET ), $ words ["prev " ], '' , '' ), $ htmlButton );
359
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array (" rel='prev' " , $ url . "? " . http_build_query ($ _GET ), $ words ["prev " ], '' , '' ), $ htmlButton );
319
360
}
320
361
}
321
362
$ pageCount = 0 ;
@@ -324,39 +365,39 @@ public static function page($echo = true, $class = "")
324
365
if ($ pageCount == $ pageNow ) {
325
366
if (self ::$ friendlyURL ) {
326
367
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageNow );
327
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
368
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , "javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
328
369
} else {
329
370
$ _GET [$ words ['url ' ]] = $ pageNow ;
330
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
371
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , "javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
331
372
}
332
373
} else {
333
374
if ($ pageCount <= $ pageNow && ($ pageCount >= $ pageNow - 4 && ($ pageNow == $ totalPages || $ pageNow + 1 == $ totalPages || $ pageNow + 2 == $ totalPages ) || ($ pageCount >= $ pageNow - 2 )) && $ pageCount > 0 && count ($ buttons ) < 5 && ($ pageCount == $ pageNow - 1 || $ pageCount == $ pageNow - 2 || count ($ buttons ) <= 5 )) {
334
375
if (self ::$ friendlyURL ) {
335
376
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageCount );
336
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "/ " . implode ("/ " , $ parts ), $ pageCount , '' , '' ), $ htmlButton );
377
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , $ url . "/ " . implode ("/ " , $ parts ), $ pageCount , '' , '' ), $ htmlButton );
337
378
} else {
338
379
$ _GET [$ words ['url ' ]] = $ pageCount ;
339
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "? " . http_build_query ($ _GET ), $ pageCount , '' , '' ), $ htmlButton );
380
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , $ url . "? " . http_build_query ($ _GET ), $ pageCount , '' , '' ), $ htmlButton );
340
381
}
341
382
}
342
383
if ($ pageCount >= $ pageNow && $ pageCount <= $ totalPages && count ($ buttons ) <= 5 && ($ pageCount == $ pageNow + 1 || $ pageCount == $ pageNow + 2 || count ($ buttons ) <= 5 )) {
343
384
if (self ::$ friendlyURL ) {
344
385
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageCount );
345
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "/ " . implode ("/ " , $ parts ), $ pageCount , '' , '' ), $ htmlButton );
386
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , $ url . "/ " . implode ("/ " , $ parts ), $ pageCount , '' , '' ), $ htmlButton );
346
387
} else {
347
388
$ _GET [$ words ['url ' ]] = $ pageCount ;
348
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "? " . http_build_query ($ _GET ), $ pageCount , '' , '' ), $ htmlButton );
389
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , $ url . "? " . http_build_query ($ _GET ), $ pageCount , '' , '' ), $ htmlButton );
349
390
}
350
391
}
351
392
}
352
393
}
353
394
if ($ pageNow < $ totalPages ) {
354
395
if (self ::$ friendlyURL ) {
355
396
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageNow + 1 );
356
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "/ " . implode ("/ " , $ parts ), $ words ["next " ], '' , '' ), $ htmlButton );
397
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array (" rel='next' " , $ url . "/ " . implode ("/ " , $ parts ), $ words ["next " ], '' , '' ), $ htmlButton );
357
398
} else {
358
399
$ _GET [$ words ['url ' ]] = $ pageNow + 1 ;
359
- $ buttons [] = str_replace (array ("{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ($ url . "? " . http_build_query ($ _GET ), $ words ["next " ], '' , '' ), $ htmlButton );
400
+ $ buttons [] = str_replace (array ("{rel} " , " { target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array (" rel='next' " , $ url . "? " . http_build_query ($ _GET ), $ words ["next " ], '' , '' ), $ htmlButton );
360
401
}
361
402
}
362
403
if ($ echo ) {
@@ -367,16 +408,19 @@ public static function page($echo = true, $class = "")
367
408
}
368
409
}
369
410
411
+ /** Returns server date */
370
412
public static function date ($ time = false )
371
413
{
372
414
return ($ time ? date ("Y-m-d " , $ time ) : date ("Y-m-d " ));
373
415
}
374
416
417
+ /** Returns server dateTime */
375
418
public static function dateTime ($ time = false )
376
419
{
377
420
return ($ time ? date ("Y-m-d H:s:i " , $ time ) : date ("Y-m-d H:s:i " ));
378
421
}
379
422
423
+ /** It will change the whole database $collation */
380
424
public static function setCollation ($ collation , $ show = false , $ execute = false )
381
425
{
382
426
$ db_nome = self ::$ connections [self ::$ connectionName ]['NAME ' ];
@@ -403,17 +447,20 @@ public static function setCollation($collation, $show = false, $execute = false)
403
447
}
404
448
}
405
449
450
+ /** Will define the $friendlyURL instance on the class */
406
451
public static function setFriendlyURL ($ FriendlyURLInstance )
407
452
{
408
453
self ::$ friendlyURL = $ FriendlyURLInstance ;
409
454
return new static ();
410
455
}
411
456
457
+ /** It will retrieve the collumns of the given $table */
412
458
private static function getTableCollumns ($ table )
413
459
{
414
460
return self ::fetchAll (self ::query ("DESCRIBE $ table " ));
415
461
}
416
462
463
+ /** It will fix invalid $data keys before insert them on the $table. It removes not used keys inside $data, and bind a empty string if a specific $key that exists on the table, doesn't exist on $data */
417
464
private static function fixDataCollumns ($ data , $ table , &$ newData = array (), $ mode = "insert " )
418
465
{
419
466
$ collumns = self ::getTableCollumns ($ table );
@@ -465,21 +512,25 @@ private static function fixDataCollumns($data, $table, &$newData = array(), $mod
465
512
return $ newData ;
466
513
}
467
514
515
+ /** It will prepare a given $sql to the given $object */
468
516
public static function prepare ($ object , $ sql )
469
517
{
470
518
return $ object ->prepare ($ sql );
471
519
}
472
520
521
+ /** It will set the given $value on the specific $key, inside the $object. Object is a PDO Statement */
473
522
public static function set (&$ object , $ key , $ value )
474
523
{
475
524
$ object ->bindValue (": " . $ key , $ value );
476
525
}
477
526
527
+ /** It executes any given $sql */
478
528
public static function execute ($ sql )
479
529
{
480
530
return self ::getInstance ()->exec ($ sql );
481
531
}
482
532
533
+ /** WORK IN PROGRESS - It will insert $data on a specific $table. The $rules are optional */
483
534
public static function insert ($ data , $ table , $ rules = array ())
484
535
{
485
536
self ::fixDataCollumns ($ data , $ table , $ newData );
@@ -498,11 +549,13 @@ public static function insert($data, $table, $rules = array())
498
549
}
499
550
}
500
551
552
+ /** It will return the id of the last inserted row */
501
553
public static function id ()
502
554
{
503
555
return self ::$ id ;
504
556
}
505
557
558
+ /** It will update a row on a specific $table with new $data. The row must attend to the $rules */
506
559
public static function update ($ data , $ table , $ rules = array ())
507
560
{
508
561
self ::fixDataCollumns ($ data , $ table , $ newData , "update " );
@@ -535,6 +588,7 @@ public static function update($data, $table, $rules = array())
535
588
}
536
589
}
537
590
591
+ /** WORK IN PROGRESS - It will delete a record - or all - on a specific $table */
538
592
public static function delete ($ table , $ rules = array ())
539
593
{
540
594
if (!empty ($ rules )) {
@@ -558,6 +612,7 @@ public static function delete($table, $rules = array())
558
612
}
559
613
}
560
614
615
+ /** It will normalize a string to be accepted on URL addresses */
561
616
public function URLNormalize ($ string )
562
617
{
563
618
$ string = preg_replace ('/[áàãâä]/ui ' , 'a ' , $ string );
@@ -572,22 +627,31 @@ public function URLNormalize($string)
572
627
}
573
628
}
574
629
630
+ /** DB Object class - it will store the result of the request */
575
631
class dbObject
576
632
{
633
+ /** It is the DB instance */
577
634
protected static $ instance = null ;
635
+
636
+ /** Array with extra info */
578
637
public $ extra = null ;
579
638
639
+ /** It already sets a number of info on $extra */
580
640
public function __construct ($ instance , $ extra = array ())
581
641
{
582
642
$ this ->setInstance ($ instance );
583
643
$ this ->extra = $ extra ;
584
644
$ this ->extra ['totalEntries ' ] = $ instance ->rowCount ();
585
645
return $ this ;
586
646
}
647
+
648
+ /** It will set the $instance */
587
649
private static function setInstance ($ instance )
588
650
{
589
651
self ::$ instance = $ instance ;
590
652
}
653
+
654
+ /** It will retrieve the object's DB $instance */
591
655
public static function getInstance ()
592
656
{
593
657
return self ::$ instance ;
0 commit comments