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