Nouveaux constructeurs pour les classes

This commit is contained in:
galaxyoyo 2019-09-06 14:02:32 +02:00
parent a1ef162bdb
commit b5d567e364
3 changed files with 102 additions and 31 deletions

View File

@ -16,7 +16,9 @@ class Team
private $access_code; private $access_code;
private $year; private $year;
public function __construct($id) private function __construct() {}
public static function fromId($id)
{ {
global $DB; global $DB;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `id` = ?;"); $req = $DB->prepare("SELECT * FROM `teams` WHERE `id` = ?;");
@ -26,7 +28,29 @@ class Team
if ($data === false) if ($data === false)
throw new InvalidArgumentException("L'équipe spécifiée n'existe pas."); throw new InvalidArgumentException("L'équipe spécifiée n'existe pas.");
$this->id = $id; $team = new Team();
$team->fill($data);
return $team;
}
public static function fromTrigram($trigram)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `trigram` = ? AND `year` = $YEAR;");
$req->execute([htmlspecialchars($trigram)]);
$data = $req->fetch();
if ($data === false)
throw new InvalidArgumentException("L'équipe spécifiée n'existe pas.");
$team = new Team();
$team->fill($data);
return $team;
}
private function fill($data)
{
$this->id = $data["id"];
$this->name = $data["name"]; $this->name = $data["name"];
$this->trigram = $data["trigram"]; $this->trigram = $data["trigram"];
$this->tournament = $data["tournament"]; $this->tournament = $data["tournament"];

View File

@ -17,9 +17,11 @@ class Tournament
private $final; private $final;
private $year; private $year;
public function __construct($id) private function __construct() {}
{
global $DB; public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `id` = ?;"); $req = $DB->prepare("SELECT * FROM `tournaments` WHERE `id` = ?;");
$req->execute([htmlspecialchars($id)]); $req->execute([htmlspecialchars($id)]);
$data = $req->fetch(); $data = $req->fetch();
@ -27,7 +29,29 @@ class Tournament
if ($data === false) if ($data === false)
throw new InvalidArgumentException("Le tournoi spécifié n'existe pas."); throw new InvalidArgumentException("Le tournoi spécifié n'existe pas.");
$this->id = $id; $tournament = new Tournament();
$tournament->fill($data);
return $tournament;
}
public static function fromName($name)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `name` = ? AND `year` = $YEAR;");
$req->execute([htmlspecialchars($name)]);
$data = $req->fetch();
if ($data === false)
throw new InvalidArgumentException("Le tournoi spécifié n'existe pas.");
$tournament = new Tournament();
$tournament->fill($data);
return $tournament;
}
private function fill($data)
{
$this->id = $data["id"];
$this->name = $data["name"]; $this->name = $data["name"];
$this->size = $data["size"]; $this->size = $data["size"];
$this->place = $data["place"]; $this->place = $data["place"];
@ -40,7 +64,7 @@ class Tournament
$this->date_syntheses = $data["date_syntheses"]; $this->date_syntheses = $data["date_syntheses"];
$this->final = $data["final"] == true; $this->final = $data["final"] == true;
$this->year = $data["year"]; $this->year = $data["year"];
} }
public function getId() public function getId()
{ {

View File

@ -27,8 +27,10 @@ class User
private $year; private $year;
private $confirm_email; private $confirm_email;
private $forgotten_password; private $forgotten_password;
private function __construct() {}
public function __construct($id) public static function fromId($id)
{ {
global $DB; global $DB;
$req = $DB->prepare("SELECT * FROM `users` WHERE `id` = ?;"); $req = $DB->prepare("SELECT * FROM `users` WHERE `id` = ?;");
@ -38,7 +40,29 @@ class User
if ($data === false) if ($data === false)
throw new InvalidArgumentException("L'utilisateur spécifié n'existe pas."); throw new InvalidArgumentException("L'utilisateur spécifié n'existe pas.");
$this->id = $id; $user = new User();
$user->fill($data);
return $user;
}
public static function fromEmail($email)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT * FROM `users` WHERE `email` = ? AND `year` = $YEAR;");
$req->execute([htmlspecialchars($email)]);
$data = $req->fetch();
if ($data === false)
throw new InvalidArgumentException("L'utilisateur spécifié n'existe pas.");
$user = new User();
$user->fill($data);
return $user;
}
private function fill($data)
{
$this->id = $data["id"];
$this->email = $data["email"]; $this->email = $data["email"];
$this->pwd_hash = $data["pwd_hash"]; $this->pwd_hash = $data["pwd_hash"];
$this->surname = $data["surname"]; $this->surname = $data["surname"];
@ -61,7 +85,6 @@ class User
$this->year = $data["year"]; $this->year = $data["year"];
$this->confirm_email = $data["confirm_email"]; $this->confirm_email = $data["confirm_email"];
$this->forgotten_password = $data["forgotten_password"]; $this->forgotten_password = $data["forgotten_password"];
} }
public function getEmail() public function getEmail()
@ -73,7 +96,7 @@ class User
{ {
global $DB; global $DB;
$this->email = $email; $this->email = $email;
$DB->prepare("UPDATE `users` SET `email` = ?, WHERE `id` = ?;")->execute([$email, $this->getId()]); $DB->prepare("UPDATE `users` SET `email` = ? WHERE `id` = ?;")->execute([$email, $this->getId()]);
} }
public function getId() public function getId()
@ -95,7 +118,7 @@ class User
{ {
global $DB; global $DB;
$this->pwd_hash = $password_hash; $this->pwd_hash = $password_hash;
$DB->prepare("UPDATE `users` SET `pwd_hash` = ?, WHERE `id` = ?;")->execute([$password_hash, $this->getId()]); $DB->prepare("UPDATE `users` SET `pwd_hash` = ? WHERE `id` = ?;")->execute([$password_hash, $this->getId()]);
} }
public function getSurname() public function getSurname()
@ -107,7 +130,7 @@ class User
{ {
global $DB; global $DB;
$this->surname = $surname; $this->surname = $surname;
$DB->prepare("UPDATE `users` SET `surname` = ?, WHERE `id` = ?;")->execute([$surname, $this->getId()]); $DB->prepare("UPDATE `users` SET `surname` = ? WHERE `id` = ?;")->execute([$surname, $this->getId()]);
} }
public function getFirstName() public function getFirstName()
@ -119,7 +142,7 @@ class User
{ {
global $DB; global $DB;
$this->first_name = $first_name; $this->first_name = $first_name;
$DB->prepare("UPDATE `users` SET `first_name` = ?, WHERE `id` = ?;")->execute([$first_name, $this->getId()]); $DB->prepare("UPDATE `users` SET `first_name` = ? WHERE `id` = ?;")->execute([$first_name, $this->getId()]);
} }
public function getBirthDate() public function getBirthDate()
@ -131,7 +154,7 @@ class User
{ {
global $DB; global $DB;
$this->birth_date = $birth_date; $this->birth_date = $birth_date;
$DB->prepare("UPDATE `users` SET `birth_date` = ?, WHERE `id` = ?;")->execute([$birth_date, $this->getId()]); $DB->prepare("UPDATE `users` SET `birth_date` = ? WHERE `id` = ?;")->execute([$birth_date, $this->getId()]);
} }
public function getGender() public function getGender()
@ -143,7 +166,7 @@ class User
{ {
global $DB; global $DB;
$this->gender = $gender; $this->gender = $gender;
$DB->prepare("UPDATE `users` SET `email` = ?, WHERE `id` = ?;")->execute([$gender, $this->getId()]); $DB->prepare("UPDATE `users` SET `email` = ? WHERE `id` = ?;")->execute([$gender, $this->getId()]);
} }
public function getAddress() public function getAddress()
@ -155,7 +178,7 @@ class User
{ {
global $DB; global $DB;
$this->address = $address; $this->address = $address;
$DB->prepare("UPDATE `users` SET `address` = ?, WHERE `id` = ?;")->execute([$address, $this->getId()]); $DB->prepare("UPDATE `users` SET `address` = ? WHERE `id` = ?;")->execute([$address, $this->getId()]);
} }
public function getPostalCode() public function getPostalCode()
@ -167,7 +190,7 @@ class User
{ {
global $DB; global $DB;
$this->postal_code = $postal_code; $this->postal_code = $postal_code;
$DB->prepare("UPDATE `users` SET `postal_code` = ?, WHERE `id` = ?;")->execute([$postal_code, $this->getId()]); $DB->prepare("UPDATE `users` SET `postal_code` = ? WHERE `id` = ?;")->execute([$postal_code, $this->getId()]);
} }
public function getCity() public function getCity()
@ -179,7 +202,7 @@ class User
{ {
global $DB; global $DB;
$this->city = $city; $this->city = $city;
$DB->prepare("UPDATE `users` SET `city` = ?, WHERE `id` = ?;")->execute([$city, $this->getId()]); $DB->prepare("UPDATE `users` SET `city` = ? WHERE `id` = ?;")->execute([$city, $this->getId()]);
} }
public function getCountry() public function getCountry()
@ -191,7 +214,7 @@ class User
{ {
global $DB; global $DB;
$this->country = $country; $this->country = $country;
$DB->prepare("UPDATE `users` SET `country` = ?, WHERE `id` = ?;")->execute([$country, $this->getId()]); $DB->prepare("UPDATE `users` SET `country` = ? WHERE `id` = ?;")->execute([$country, $this->getId()]);
} }
public function getPhoneNumber() public function getPhoneNumber()
@ -203,7 +226,7 @@ class User
{ {
global $DB; global $DB;
$this->phone_number = $phone_number; $this->phone_number = $phone_number;
$DB->prepare("UPDATE `users` SET `phone_number` = ?, WHERE `id` = ?;")->execute([$phone_number, $this->getId()]); $DB->prepare("UPDATE `users` SET `phone_number` = ? WHERE `id` = ?;")->execute([$phone_number, $this->getId()]);
} }
public function getSchool() public function getSchool()
@ -215,7 +238,7 @@ class User
{ {
global $DB; global $DB;
$this->school = $school; $this->school = $school;
$DB->prepare("UPDATE `users` SET `school` = ?, WHERE `id` = ?;")->execute([$school, $this->getId()]); $DB->prepare("UPDATE `users` SET `school` = ? WHERE `id` = ?;")->execute([$school, $this->getId()]);
} }
public function getClass() public function getClass()
@ -227,7 +250,7 @@ class User
{ {
global $DB; global $DB;
$this->class = $class; $this->class = $class;
$DB->prepare("UPDATE `users` SET `class` = ?, WHERE `id` = ?;")->execute([$class, $this->getId()]); $DB->prepare("UPDATE `users` SET `class` = ? WHERE `id` = ?;")->execute([$class, $this->getId()]);
} }
public function getResponsibleName() public function getResponsibleName()
@ -239,7 +262,7 @@ class User
{ {
global $DB; global $DB;
$this->responsible_name = $responsible_name; $this->responsible_name = $responsible_name;
$DB->prepare("UPDATE `users` SET `responsible_name` = ?, WHERE `id` = ?;")->execute([$responsible_name, $this->getId()]); $DB->prepare("UPDATE `users` SET `responsible_name` = ? WHERE `id` = ?;")->execute([$responsible_name, $this->getId()]);
} }
public function getResponsiblePhone() public function getResponsiblePhone()
@ -251,7 +274,7 @@ class User
{ {
global $DB; global $DB;
$this->responsible_phone = $responsible_phone; $this->responsible_phone = $responsible_phone;
$DB->prepare("UPDATE `users` SET `responsible_phone` = ?, WHERE `id` = ?;")->execute([$responsible_phone, $this->getId()]); $DB->prepare("UPDATE `users` SET `responsible_phone` = ? WHERE `id` = ?;")->execute([$responsible_phone, $this->getId()]);
} }
public function getResponsibleEmail() public function getResponsibleEmail()
@ -263,7 +286,7 @@ class User
{ {
global $DB; global $DB;
$this->responsible_email = $responsible_email; $this->responsible_email = $responsible_email;
$DB->prepare("UPDATE `users` SET `responsible_email` = ?, WHERE `id` = ?;")->execute([$responsible_email, $this->getId()]); $DB->prepare("UPDATE `users` SET `responsible_email` = ? WHERE `id` = ?;")->execute([$responsible_email, $this->getId()]);
} }
public function getDescription() public function getDescription()
@ -275,7 +298,7 @@ class User
{ {
global $DB; global $DB;
$this->description = $desc; $this->description = $desc;
$DB->prepare("UPDATE `users` SET `description` = ?, WHERE `id` = ?;")->execute([$desc, $this->getId()]); $DB->prepare("UPDATE `users` SET `description` = ? WHERE `id` = ?;")->execute([$desc, $this->getId()]);
} }
public function getRole() public function getRole()
@ -288,7 +311,7 @@ class User
global $DB; global $DB;
$this->role = $role; $this->role = $role;
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
$DB->prepare("UPDATE `users` SET `email` = ?, WHERE `id` = ?;")->execute([$role->getName(), $this->getId()]); $DB->prepare("UPDATE `users` SET `email` = ? WHERE `id` = ?;")->execute([$role->getName(), $this->getId()]);
} }
public function getTeamId() public function getTeamId()
@ -300,7 +323,7 @@ class User
{ {
global $DB; global $DB;
$this->team_id = $team_id; $this->team_id = $team_id;
$DB->prepare("UPDATE `users` SET `team_id` = ?, WHERE `id` = ?;")->execute([$team_id, $this->getId()]); $DB->prepare("UPDATE `users` SET `team_id` = ? WHERE `id` = ?;")->execute([$team_id, $this->getId()]);
} }
public function getYear() public function getYear()
@ -317,7 +340,7 @@ class User
{ {
global $DB; global $DB;
$this->confirm_email = $token; $this->confirm_email = $token;
$DB->prepare("UPDATE `users` SET `confirm_email` = ?, WHERE `id` = ?;")->execute([$token, $this->getId()]); $DB->prepare("UPDATE `users` SET `confirm_email` = ? WHERE `id` = ?;")->execute([$token, $this->getId()]);
} }
public function getForgottenPasswordToken() public function getForgottenPasswordToken()
@ -329,6 +352,6 @@ class User
{ {
global $DB; global $DB;
$this->forgotten_password = $token; $this->forgotten_password = $token;
$DB->prepare("UPDATE `users` SET `forgotten_password` = ?, WHERE `id` = ?;")->execute([$token, $this->getId()]); $DB->prepare("UPDATE `users` SET `forgotten_password` = ? WHERE `id` = ?;")->execute([$token, $this->getId()]);
} }
} }