con = $con; } /** * get tactic information from given identifier * @param int $id * @return TacticInfo|null */ public function get(int $id): ?TacticInfo { $res = $this->con->fetch( "SELECT * FROM Tactic WHERE id = :id", [":id" => [$id, PDO::PARAM_INT]] ); if (!isset($res[0])) { return null; } $row = $res[0]; return new TacticInfo($id, $row["name"], strtotime($row["creation_date"]), $row["owner"], $row['content']); } /** * Return the nb last tactics created * * @param integer $nb * @return array> */ public function getLast(int $nb): ?array { $res = $this->con->fetch( "SELECT * FROM Tactic ORDER BY creation_date DESC LIMIT :nb ", [":nb" => [$nb, PDO::PARAM_INT]] ); if (count($res) == 0) { return null; } return $res; } /** * @param string $name * @param int $owner * @return int inserted tactic id */ public function insert(string $name, int $owner): int { $this->con->exec( "INSERT INTO Tactic(name, owner) VALUES(:name, :owner)", [ ":name" => [$name, PDO::PARAM_STR], ":owner" => [$owner, PDO::PARAM_INT], ] ); return intval($this->con->lastInsertId()); } /** * update name of given tactic identifier * @param int $id * @param string $name * @return bool */ public function updateName(int $id, string $name): bool { $stmnt = $this->con->prepare("UPDATE Tactic SET name = :name WHERE id = :id"); $stmnt->execute([ ":name" => $name, ":id" => $id, ]); return $stmnt->rowCount() == 1; } /*** * Updates a given tactics content * @param int $id * @param string $json * @return bool */ public function updateContent(int $id, string $json): bool { $stmnt = $this->con->prepare("UPDATE Tactic SET content = :content WHERE id = :id"); $stmnt->execute([ ":content" => $json, ":id" => $id, ]); return $stmnt->rowCount() == 1; } }