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