id = $id; $this->user = $user; $this->name = $name; $this->content = $content; $this->postedAt = new DateTime($postedAt); } /* * Statics */ public static function list(int $page, int $limit, bool $authed = false) { $db = Database::getInstance(); $stmt = $db->prepare( "SELECT COUNT(*) FROM egb_gaestebuch" ); $stmt->execute(); $count = $stmt->fetch(PDO::FETCH_COLUMN, 0); $stmt = $db->prepare( "SELECT * FROM egb_gaestebuch LIMIT $limit OFFSET " . ($page * $limit) ); $stmt->execute(); $data = $stmt->fetchAll(); $list = array_map( function ($item) use ($authed) { $user = User::getByID($item["benutzer_id"]); return new Post($item["id"], $authed ? $user : null, !$authed ? $user->getUsername() : null, $item["beitrag"], $item["zeitstempel"]); }, $data ); return ["pages" => intdiv($count, $limit) + 1, "data" => $list]; } /* * Getters */ public function getId(): int { return $this->id; } public function getUser(): User { return $this->user; } public function getContent(): string { return $this->content; } public function getPostedAt(): DateTime { return $this->postedAt; } /* * JSON */ public function jsonSerialize(): array { $user = $this->user ? $this->user : [ "username" => $this->name, ]; return [ 'id' => $this->id, 'user' => $user, 'content' => $this->content, 'postedAt' => $this->postedAt, ]; } }