1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stonks\DataLayer; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
use PDOException; |
8
|
|
|
use stdClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait CrudTrait |
12
|
|
|
* |
13
|
|
|
* @package Stonks\DataLayer |
14
|
|
|
*/ |
15
|
|
|
trait CrudTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Exception|PDOException|null |
20
|
|
|
*/ |
21
|
|
|
private $fail; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $data |
25
|
|
|
* @return int|string|null |
26
|
|
|
*/ |
27
|
|
|
private function create(array $data) |
28
|
|
|
{ |
29
|
|
|
if ((is_bool($this->timestamps) && $this->timestamps) || $this->timestamps === 'created_at') { |
30
|
|
|
$data['created_at'] = (new DateTime('now'))->format('Y-m-d H:i:s'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
$connect = Connect::testConnection($this->database); |
35
|
|
|
$treatData = $this->treatDataCreate($data); |
36
|
|
|
|
37
|
|
|
$statement = $connect->prepare("INSERT INTO {$this->entity} ({$treatData->columns}) VALUES ({$treatData->values})"); |
38
|
|
|
$statement->execute($this->filter($treatData->data)); |
39
|
|
|
|
40
|
|
|
$lastInsertId = $connect->lastInsertId(); |
41
|
|
|
$primary = $this->primary; |
42
|
|
|
|
43
|
|
|
return ($lastInsertId ? $lastInsertId : $this->data->$primary); |
44
|
|
|
} catch (PDOException $exception) { |
45
|
|
|
$this->fail = $exception; |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $data |
52
|
|
|
* @param string $terms |
53
|
|
|
* @param string $params |
54
|
|
|
* @return int|null |
55
|
|
|
*/ |
56
|
|
|
private function update(array $data, string $terms, string $params): ?int |
57
|
|
|
{ |
58
|
|
|
if ((is_bool($this->timestamps) && $this->timestamps) || $this->timestamps === 'updated_at') { |
59
|
|
|
$data['updated_at'] = (new DateTime('now'))->format('Y-m-d H:i:s'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$connect = Connect::testConnection($this->database); |
64
|
|
|
$treatData = $this->treatDataUpdate($data); |
65
|
|
|
|
66
|
|
|
parse_str($params, $arrParams); |
67
|
|
|
|
68
|
|
|
$statement = $connect->prepare("UPDATE {$this->entity} SET {$treatData->values} WHERE {$terms}"); |
69
|
|
|
$statement->execute($this->filter(array_merge($treatData->data, $arrParams))); |
70
|
|
|
|
71
|
|
|
return ($statement->rowCount() ?? 1); |
72
|
|
|
} catch (PDOException $exception) { |
73
|
|
|
$this->fail = $exception; |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $terms |
80
|
|
|
* @param string|null $params |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
private function delete(string $terms, ?string $params): bool |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$connect = Connect::testConnection($this->database); |
87
|
|
|
$statement = $connect->prepare("DELETE FROM {$this->entity} WHERE {$terms}"); |
88
|
|
|
|
89
|
|
|
if ($params) { |
90
|
|
|
parse_str($params, $arrParams); |
91
|
|
|
$statement->execute($arrParams); |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$statement->execute(); |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} catch (PDOException $exception) { |
99
|
|
|
$this->fail = $exception; |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $data |
106
|
|
|
* @return array|null |
107
|
|
|
*/ |
108
|
|
|
private function filter(array $data): ?array |
109
|
|
|
{ |
110
|
|
|
$filter = []; |
111
|
|
|
|
112
|
|
|
foreach ($data as $key => $value) { |
113
|
|
|
$filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $filter; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $data |
121
|
|
|
* @return object |
122
|
|
|
*/ |
123
|
|
|
private function treatDataCreate(array $data): object |
124
|
|
|
{ |
125
|
|
|
$treatData = new stdClass(); |
126
|
|
|
$treatData->data = $data; |
127
|
|
|
$treatData->columns = ''; |
128
|
|
|
$treatData->values = ''; |
129
|
|
|
|
130
|
|
|
if ($this->functionSql) { |
131
|
|
|
foreach ($this->functionSql as $key => $value) { |
132
|
|
|
if (array_key_exists($key, $treatData->data)) { |
133
|
|
|
unset($treatData->data[$key]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$data = array_merge($this->functionSql, $data); |
138
|
|
|
$treatData->values .= implode(', ', array_values($this->functionSql)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$treatData->columns = implode(', ', array_keys($data)); |
142
|
|
|
$treatData->values .= ($treatData->values ? ', ' : '') . ':' . implode(', :', array_keys($treatData->data)); |
143
|
|
|
|
144
|
|
|
return $treatData; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $data |
149
|
|
|
* @param array $dataSet |
150
|
|
|
* @return object |
151
|
|
|
*/ |
152
|
|
|
private function treatDataUpdate(array $data, array $dataSet = []): object |
153
|
|
|
{ |
154
|
|
|
$treatData = new stdClass(); |
155
|
|
|
$treatData->data = $data; |
156
|
|
|
$treatData->values = ''; |
157
|
|
|
|
158
|
|
|
if ($this->functionSql) { |
159
|
|
|
foreach ($this->functionSql as $key => $value) { |
160
|
|
|
if (is_array($dataSet)) { |
161
|
|
|
$dataSet[] = "{$key} = {$this->functionSql[$key]}"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (array_key_exists($key, $treatData->data)) { |
165
|
|
|
unset($treatData->data[$key]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
foreach ($treatData->data as $bind => $value) { |
171
|
|
|
$dataSet[] = "{$bind} = :{$bind}"; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$treatData->values .= implode(', ', $dataSet); |
175
|
|
|
|
176
|
|
|
return $treatData; |
177
|
|
|
} |
178
|
|
|
} |