Skip to content

Commit 5000d61

Browse files
committed
v 3.1.0
1 parent 68fdbee commit 5000d61

File tree

4 files changed

+370
-56
lines changed

4 files changed

+370
-56
lines changed

Database.php

+171-27
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* please don't remove this comment block
55
*
66
* @author phptricks Team - Mohammad Anzawi
7-
* @author_uri http://phptricks.org
7+
* @author_uri https://phptricks.org
88
* @uri https://github.com/anzawi/php-database-class
9-
* @version 3.0.0
9+
* @version 3.1.0
1010
* @licence MIT -> https://opensource.org/licenses/MIT
1111
* @package PHPtricks\Database
1212
*/
@@ -30,10 +30,6 @@ class Database implements \IteratorAggregate, \ArrayAccess
3030
private static $_instance;
3131

3232
private
33-
/**
34-
* @var $_pdo object PDO object
35-
*/
36-
$_pdo,
3733
/**
3834
* @var $_query string store sql statement
3935
*/
@@ -70,6 +66,10 @@ class Database implements \IteratorAggregate, \ArrayAccess
7066

7167

7268
protected
69+
/**
70+
* @var $_pdo object PDO object
71+
*/
72+
$_pdo,
7373
/**
7474
* @var $_table string current table name
7575
*/
@@ -105,13 +105,23 @@ protected function __construct($data = [], $info = [])
105105
}
106106
}
107107

108+
// public function __call($method, $args)
109+
// {
110+
// var_dump($this);
111+
// }
112+
108113
/**
109114
* @param $prop
110115
* @return mixed
111116
*/
112117
public function __get($prop)
113118
{
114-
return isset($this->_results->$prop) ? $this->_results->$prop : null;
119+
if(!isset($this->__cach[md5($this->_table)]))
120+
{
121+
return null;
122+
}
123+
124+
return isset($this->_results->$prop) ? $this->_results->$prop : null;
115125
}
116126

117127
public function __set($prop, $value)
@@ -245,7 +255,7 @@ public static function connect()
245255
// check if $_instance is null or not
246256
// if null so connect database
247257
// otherwise return current connection object
248-
if(!isset(self::$_instance)) {
258+
if(!isset(self::$_instance) || self::$_instance == null) {
249259
self::$_instance = new Database();
250260
}
251261

@@ -399,16 +409,21 @@ public function save()
399409
{
400410
$x = 1;
401411
$this->_query = "WHERE";
402-
foreach($this->results() as $i => $row)
403-
{
404-
$this->_query .= " {$i} = {$row}";
412+
413+
foreach($this->results() as $i => $row)
414+
{
415+
if(!is_numeric($row))
416+
$this->_query .= " {$i} = '{$row}'";
417+
else
418+
$this->_query .= " {$i} = {$row}";
405419
// add comma between values
406420
if($x < count((array)$this->results())) {
407421
$this->_query .= " AND";
408422
}
409423

410424
$x++;
411425
}
426+
412427
return $this->update((array)$this->getNewValues());
413428
}
414429

@@ -431,7 +446,29 @@ public function select($fields = ['*'])
431446
. " FROM {$this->_table} {$this->_query}";
432447

433448
$this->_query = $sql;
434-
return new Database($this->query($sql)->results(), ['table' => $this->_table, 'id' => $this->_idColumn]);
449+
450+
return $this->collection([
451+
'results' => $this->query($sql)->results(),
452+
'table' => $this->_table,
453+
'id' => $this->_idColumn
454+
]);
455+
456+
// return new Database($this->query($sql)->results(), ['table' => $this->_table, 'id' => $this->_idColumn]);
457+
}
458+
459+
protected function collection($collection)
460+
{
461+
return new Collection($collection, self::$_instance);
462+
}
463+
464+
protected function getCollection($table)
465+
{
466+
if(isset($this->__cach[md5($table)]))
467+
{
468+
return true;
469+
}
470+
471+
return false;
435472
}
436473

437474
/**
@@ -440,14 +477,15 @@ public function select($fields = ['*'])
440477
*/
441478
public function delete()
442479
{
480+
$results = (array)$this->_results;
443481
if($this->count() == 1)
444482
{
445-
return $this->remove($this->_results);
483+
return $this->remove($results);
446484
}
447485

448486
for($i = 0; $this->count() > $i; $i++)
449487
{
450-
$this->remove($this->_results[$i]);
488+
$this->remove( $results[$i]);
451489
}
452490

453491
return true;
@@ -457,9 +495,13 @@ private function remove($data)
457495
{
458496
$this->_where = "WHERE";
459497
$x = 1;
498+
460499
foreach($data as $i => $row)
461500
{
462-
$this->_where .= " {$i} = {$row}";
501+
if(!is_numeric($row))
502+
$this->_where .= " {$i} = '{$row}'";
503+
else
504+
$this->_where .= " {$i} = {$row}";
463505
// add comma between values
464506
if($x < count((array)$data)) {
465507
$this->_where .= " AND";
@@ -477,16 +519,8 @@ private function remove($data)
477519
*/
478520
public function find($id)
479521
{
480-
$result = $this->where($this->_idColumn, $id)
481-
->first()->results();
482-
483-
484-
if(count((array)$result))
485-
{
486-
return new Database($result, ['id' => $this->_idColumn, 'table' => $this->_table]);
487-
}
488-
489-
return new Database([], ['id' => $this->_idColumn, 'table' => $this->_table]);
522+
return $result = $this->where($this->_idColumn, $id)
523+
->first();
490524
}
491525

492526
/**
@@ -622,9 +656,18 @@ public function first()
622656

623657
if(count((array)$results))
624658
{
625-
return new Database($results[0], ['table' => $this->_table, 'id' => $this->_idColumn]);
659+
return $this->collection([
660+
'results' => $results[0],
661+
'table' => $this->_table,
662+
'id' => $this->_idColumn
663+
]);
626664
}
627-
return new Database([], ['table' => $this->_table, 'id' => $this->_idColumn]);
665+
666+
return $this->collection([
667+
'results' => [],
668+
'table' => $this->_table,
669+
'id' => $this->_idColumn
670+
]);
628671
}
629672

630673
public function firstRecord()
@@ -1284,4 +1327,105 @@ private function getNewValues()
12841327
{
12851328
return $this->_newValues;
12861329
}
1330+
}
1331+
1332+
class Collection extends Database
1333+
{
1334+
public function __construct($data, $connection = null)
1335+
{
1336+
if(isset($connection))
1337+
{
1338+
$this->_table = $data['table'];
1339+
$this->_results = $data['results'];
1340+
$this->_idColumn = $data['id'];
1341+
$this->_pdo = $connection->_pdo;
1342+
}
1343+
else
1344+
$this->_results = $data;
1345+
}
1346+
1347+
public function all()
1348+
{
1349+
return $this->results();
1350+
}
1351+
1352+
public function empty()
1353+
{
1354+
return empty($this->_results);
1355+
}
1356+
1357+
public function first()
1358+
{
1359+
return isset($this->_results[0]) ? $this->_results[0] : null;
1360+
}
1361+
1362+
public function last()
1363+
{
1364+
$reverse = array_reverse($this->results());
1365+
1366+
return isset($reverse[0]) ? $reverse[0] : null;
1367+
}
1368+
1369+
public function each(callable $callback)
1370+
{
1371+
foreach ($this->results() as $key => $value)
1372+
{
1373+
$callback($value, $key);
1374+
}
1375+
1376+
return $this;
1377+
}
1378+
1379+
public function filter(callable $callback = null)
1380+
{
1381+
if($callback)
1382+
{
1383+
return new static(array_filter($this->results(), $callback));
1384+
}
1385+
1386+
// exclude null and empty
1387+
return new static(array_filter($this->results()));
1388+
}
1389+
1390+
public function keys()
1391+
{
1392+
return new static(array_keys($this->results()));
1393+
}
1394+
1395+
public function map(callable $callback)
1396+
{
1397+
$keys = $this->keys()->all();
1398+
$results = array_map($callback, $this->results(), $keys);
1399+
1400+
return new static(array_combine($keys, $results));
1401+
}
1402+
1403+
1404+
public function toJson()
1405+
{
1406+
return json_encode($this->results());
1407+
}
1408+
1409+
public function __toString()
1410+
{
1411+
return $this->toJson();
1412+
}
1413+
1414+
public function merge($items)
1415+
{
1416+
return new static(
1417+
array_merge(
1418+
$this->results(),
1419+
$this->toArray($items)
1420+
)
1421+
);
1422+
}
1423+
1424+
protected function toArray($items)
1425+
{
1426+
if(!is_array($items) && $items instanceof Collection)
1427+
return $items->all();
1428+
1429+
return $items;
1430+
}
12871431
}

0 commit comments

Comments
 (0)