1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodefusionTM\DataLayer; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
use PDOException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait DataLayerTrait |
11
|
|
|
* @package CodefusionTM\DataLayer |
12
|
|
|
*/ |
13
|
|
|
trait DataLayerTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $data |
17
|
|
|
* @return int|null |
18
|
|
|
* @throws Exception |
19
|
|
|
*/ |
20
|
|
|
protected function create(array $data): ?int |
21
|
|
|
{ |
22
|
|
|
if ($this->timestamps) { |
23
|
|
|
$data["created_at"] = (new DateTime("now"))->format("Y-m-d H:i:s"); |
24
|
|
|
$data["updated_at"] = $data["created_at"]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
$columns = implode(", ", array_keys($data)); |
29
|
|
|
$values = ":" . implode(", :", array_keys($data)); |
30
|
|
|
|
31
|
|
|
$stmt = Connect::getInstance()->prepare("INSERT INTO {$this->entity} ({$columns}) VALUES ({$values})"); |
32
|
|
|
$stmt->execute($this->filter($data)); |
33
|
|
|
|
34
|
|
|
return Connect::getInstance()->lastInsertId(); |
|
|
|
|
35
|
|
|
} catch (PDOException $exception) { |
36
|
|
|
$this->fail = $exception; |
|
|
|
|
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $data |
43
|
|
|
* @param string $terms |
44
|
|
|
* @param string $params |
45
|
|
|
* @return int|null |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
protected function update(array $data, string $terms, string $params): ?int |
49
|
|
|
{ |
50
|
|
|
if ($this->timestamps) { |
51
|
|
|
$data["updated_at"] = (new DateTime("now"))->format("Y-m-d H:i:s"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$dateSet = []; |
56
|
|
|
foreach ($data as $bind => $value) { |
57
|
|
|
$dateSet[] = "{$bind} = :{$bind}"; |
58
|
|
|
} |
59
|
|
|
$dateSet = implode(", ", $dateSet); |
60
|
|
|
parse_str($params, $params); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$stmt = Connect::getInstance()->prepare("UPDATE {$this->entity} SET {$dateSet} WHERE {$terms}"); |
63
|
|
|
$stmt->execute($this->filter(array_merge($data, $params))); |
64
|
|
|
return ($stmt->rowCount() ?? 1); |
65
|
|
|
} catch (PDOException $exception) { |
66
|
|
|
$this->fail = $exception; |
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $terms |
73
|
|
|
* @param string|null $params |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function delete(string $terms, ?string $params): bool |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$stmt = Connect::getInstance()->prepare("DELETE FROM {$this->entity} WHERE {$terms}"); |
80
|
|
|
if ($params) { |
81
|
|
|
parse_str($params, $params); |
|
|
|
|
82
|
|
|
$stmt->execute($params); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$stmt->execute(); |
87
|
|
|
return true; |
88
|
|
|
} catch (PDOException $exception) { |
89
|
|
|
$this->fail = $exception; |
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $data |
96
|
|
|
* @return array|null |
97
|
|
|
*/ |
98
|
|
|
private function filter(array $data): ?array |
99
|
|
|
{ |
100
|
|
|
$filter = []; |
101
|
|
|
foreach ($data as $key => $value) { |
102
|
|
|
$filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT)); |
103
|
|
|
} |
104
|
|
|
return $filter; |
105
|
|
|
} |
106
|
|
|
} |