setTable('tokens'); } public function getObjectProperties(): void { $this->user_id = new object_property($this->table, $this->id, 'user_id', 'int', true); $this->type = new object_property($this->table, $this->id, 'type', 'string', true); $this->token = new object_property($this->table, $this->id, 'token', 'string', true); } public function create(int $user_id, string $token, string $type = 'AUTH_TOKEN'): void { global $db; // Avoid SQL injection $token = $db->escape_string($token); // Create a new record in the database $sql = "INSERT INTO $this->table (user_id, token, type) VALUES ($user_id, '$token', '$type')"; $db->query($sql); // Get the id of the new record $this->id = $db->insert_id(); // Set the values of the object properties $this->getObjectProperties(); } /** * @throws Exception */ public function getToken(string $token): tokens_o { global $db; // Avoid SQL injection $token = $db->escape_string($token); // Prepare the SQL statement $sql = "SELECT id FROM $this->table WHERE token = '$token'"; $result = $db->query($sql); $row = $db->fetch_assoc($result); if (!$row) { throw new Exception("Token not found " . $token); } $this->id = $row['id']; $this->getObjectProperties(); return $this; } public function delete(string $token): void { global $db; // Avoid SQL injection $token = $db->escape_string($token); // Prepare the SQL statement $sql = "DELETE FROM $this->table WHERE token = '$token'"; $db->query($sql); } }