progetto fatto precedentemente adattato al framework creato con il corso
filippo.bertilotti
2024-06-05 15e03a88fa42f2444138ebc6f171c9a32a3a4238
commit | author | age
15e03a 1 <?php
F 2     namespace Framework;
3
4     use App\Database;
5     use PDO;
6     abstract class Model
7     {
8         protected array $errors = [];
9         protected ?string $table;
10
11         abstract protected function validate (array $data): void;
12         
13         public  function __construct(protected Database $database) {
14         }
15
16         public function update(string $id, array $data): bool {
17             $this->validate($data);
18             
19             if(!empty($this->errors)) {
20                 return false;
21             }
22
23             $sql = "UPDATE {$this->getTable()}";
24
25             unset($data["id"]);
26
27             $assignments = array_keys($data);
28
29             array_walk($assignments, function (&$value) {
30                 $value = "$value = ?";
31             });
32
33             $sql .= " SET " . implode(", ", $assignments);
34             $sql .= " WHERE id = ?";
35
36             $conn = $this->database->getConnection();
37             $stmt = $conn->prepare($sql);
38
39             $i = 1;
40             foreach ($data as $value) {
41                 $type = match(gettype($value)) {
42                     "boolean" => PDO::PARAM_BOOL,
43                     "integer"=> PDO::PARAM_INT,
44                     "NULL"=> PDO::PARAM_NULL,
45                     default => PDO::PARAM_STR
46                 };
47
48                 
49                 $stmt->bindValue($i++, $value, $type);
50             }
51
52             $stmt->bindValue($i, $id, PDO::PARAM_INT);
53             return $stmt->execute();
54
55         }
56
57         protected function addError(string $field, string $message): void {
58             $this->errors[$field] = $message;
59         }
60
61         public function getErrors(): array {
62             return $this->errors;
63         }
64
65         private function getTable() {
66             $parts = explode("\\", $this::class);
67             return strtolower(array_pop($parts));
68         }
69         public function getInsertID(): string {
70             $conn = $this->database->getConnection();
71             return $conn->lastInsertId();
72         }
73         public function findAll(): array|bool
74         {
75             $pdo = $this->database->getConnection();
76
77             $sql = "SELECT * FROM {$this->getTable()}";
78
79             $stmt = $pdo->query($sql);
80
81             return $stmt->fetchAll(PDO::FETCH_ASSOC);
82         }
83
84         public function find(string $id) {
85             $conn = $this->database->getConnection();
86             $sql = "SELECT *
87                     FROM {$this->getTable()}
88                     WHERE id = :id";
89             $stmt = $conn->prepare($sql);
90             $stmt->bindValue(":id", $id, PDO::PARAM_INT);
91             $stmt->execute();
92             return $stmt->fetch(PDO::FETCH_ASSOC);
93         }
94
95         public function insert(array $data) : bool {
96             $this->validate($data);
97             
98             if(!empty($this->errors)) {
99                 return false;
100             }
101             $columns = implode(", " , array_keys($data));
102             $placeholders = implode(", ", array_fill(0, count($data), "?"));
103
104             $sql = "INSERT INTO {$this->getTable()} ($columns) VALUES ($placeholders)";
105
106             $conn = $this->database->getConnection();
107             $stmt = $conn->prepare($sql);
108
109             $i = 1;
110             foreach ($data as $value) {
111                 $type = match(gettype($value)) {
112                     "boolean" => PDO::PARAM_BOOL,
113                     "integer"=> PDO::PARAM_INT,
114                     "NULL"=> PDO::PARAM_NULL,
115                     default => PDO::PARAM_STR
116                 };
117
118                 
119                 $stmt->bindValue($i++, $value, $type);
120             }
121             
122
123             return $stmt->execute();
124         }
125         
126         public function delete(string $id): bool {
127             $sql = "DELETE FROM {$this->getTable()} WHERE id = :id";
128
129             $conn = $this->database->getConnection();
130             $stmt = $conn->prepare($sql);
131             $stmt->bindValue(":id", $id, PDO::PARAM_INT);
132
133             return $stmt->execute();
134         }
135     }