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 $year;
public function __construct($id)
private function __construct() {}
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `id` = ?;");
@ -26,7 +28,29 @@ class Team
if ($data === false)
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->trigram = $data["trigram"];
$this->tournament = $data["tournament"];

View File

@ -17,9 +17,11 @@ class Tournament
private $final;
private $year;
public function __construct($id)
{
global $DB;
private function __construct() {}
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `id` = ?;");
$req->execute([htmlspecialchars($id)]);
$data = $req->fetch();
@ -27,7 +29,29 @@ class Tournament
if ($data === false)
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->size = $data["size"];
$this->place = $data["place"];
@ -40,7 +64,7 @@ class Tournament
$this->date_syntheses = $data["date_syntheses"];
$this->final = $data["final"] == true;
$this->year = $data["year"];
}
}
public function getId()
{

View File

@ -27,8 +27,10 @@ class User
private $year;
private $confirm_email;
private $forgotten_password;
private function __construct() {}
public function __construct($id)
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `users` WHERE `id` = ?;");
@ -38,7 +40,29 @@ class User
if ($data === false)
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->pwd_hash = $data["pwd_hash"];
$this->surname = $data["surname"];
@ -61,7 +85,6 @@ class User
$this->year = $data["year"];
$this->confirm_email = $data["confirm_email"];
$this->forgotten_password = $data["forgotten_password"];
}
public function getEmail()
@ -73,7 +96,7 @@ class User
{
global $DB;
$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()
@ -95,7 +118,7 @@ class User
{
global $DB;
$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()
@ -107,7 +130,7 @@ class User
{
global $DB;
$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()
@ -119,7 +142,7 @@ class User
{
global $DB;
$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()
@ -131,7 +154,7 @@ class User
{
global $DB;
$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()
@ -143,7 +166,7 @@ class User
{
global $DB;
$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()
@ -155,7 +178,7 @@ class User
{
global $DB;
$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()
@ -167,7 +190,7 @@ class User
{
global $DB;
$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()
@ -179,7 +202,7 @@ class User
{
global $DB;
$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()
@ -191,7 +214,7 @@ class User
{
global $DB;
$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()
@ -203,7 +226,7 @@ class User
{
global $DB;
$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()
@ -215,7 +238,7 @@ class User
{
global $DB;
$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()
@ -227,7 +250,7 @@ class User
{
global $DB;
$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()
@ -239,7 +262,7 @@ class User
{
global $DB;
$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()
@ -251,7 +274,7 @@ class User
{
global $DB;
$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()
@ -263,7 +286,7 @@ class User
{
global $DB;
$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()
@ -275,7 +298,7 @@ class User
{
global $DB;
$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()
@ -288,7 +311,7 @@ class User
global $DB;
$this->role = $role;
/** @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()
@ -300,7 +323,7 @@ class User
{
global $DB;
$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()
@ -317,7 +340,7 @@ class User
{
global $DB;
$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()
@ -329,6 +352,6 @@ class User
{
global $DB;
$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()]);
}
}