Skip to content
Snippets Groups Projects
Commit 695224db authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Reformatage du code et préparation au TP 7.

parent f9c23a7d
No related branches found
No related tags found
No related merge requests found
Showing
with 48 additions and 477 deletions
DB_HOST=localhost
DB_NAME=iut-dev
DB_LOGIN=iut-dev-user
DB_PASSWORD=p73i74KAV8lami2iyIpehE5ozic8GA
File deleted
<?php
/**
* @param string $index
* @return string
*/
function config(string $index) : string
{
$envPath = getcwd() . "/.env";
if(file_exists($envPath)){
$index = str_replace('_', '\\_', $index);
$envFile = file_get_contents($envPath);
preg_match("/" . $index . '=(.*?)\n/', $envFile, $matches);
$value = $matches[count($matches) - 1];
$value = preg_replace('/[^A-Za-z0-9\-]/', '', $value);
return $value;
} else {
return "";
}
}
\ No newline at end of file
<?php
require_once("app/models/View.php");
/**
* Charge le contenu d'une vue avec des paramètres.
* @param string $viewName
* @param array $args
* @return string
*/
function view(string $viewName, array $args = []): string
{
return View::load($viewName, $args);
}
\ No newline at end of file
<?php
class Database
{
// les attributs static caractéristiques de la connexion
static private $hostname = 'localhost';
static private $database = 'iut-dev';
static private $login = 'iut-dev-user';
static private $password = 'p73i74KAV8lami2iyIpehE5ozic8GA';
static private $tabUTF8 = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
// l'attribut static qui matérialisera la connexion
static private $pdo;
// le getter public de cet attribut
static public function pdo()
{
return self::$pdo;
}
// la fonction static de connexion qui initialise $pdo et lance la tentative de connexion
static public function connect()
{
$hostname = config("DB_HOST");
$database = config("DB_NAME");
$login = config("DB_LOGIN");
$password = config("DB_PASSWORD");
$t = self::$tabUTF8;
try {
self::$pdo = new PDO("mysql:host=$hostname;dbname=$database", $login, $password, $t);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Erreur de connexion : " . $e->getMessage() . "<br>";
}
}
}
?>
<?php
require_once "Objet.php";
class Adherent extends Objet
{
protected string $login;
protected string $mdp;
protected string $nomAdherent;
protected string $prenomAdherent;
protected string $email;
protected DateTime $dateAdhesion;
protected int $numCategorie;
/**
* @param string $login
* @param string $mdp
* @param string $nomAdherent
* @param string $prenomAdherent
* @param string $email
* @param DateTime $dateAdhesion
* @param int $numCategorie
*/
public function __construct(string $login, string $mdp, string $nomAdherent, string $prenomAdherent, string $email, DateTime $dateAdhesion, int $numCategorie)
{
$this->login = $login;
$this->mdp = $mdp;
$this->nomAdherent = $nomAdherent;
$this->prenomAdherent = $prenomAdherent;
$this->email = $email;
$this->dateAdhesion = $dateAdhesion;
$this->numCategorie = $numCategorie;
}
/**
* @return array
* @throws Exception
*/
public static function getAllAdherents(): array
{
// écriture de la requête
$requete = "SELECT * FROM Adherent;";
// envoi de la requête et stockage de la réponse
$resultat = Database::pdo()->query($requete);
// traitement de la réponse
$resultat->setFetchmode(PDO::FETCH_ASSOC);
$resultat = $resultat->fetchAll();
$tableau = array();
foreach ($resultat as $row){
$tableau[] = new Adherent($row['login'],
$row['mdp'],
$row['nomAdherent'],
$row['prenomAdherent'],
$row['email'],
new DateTime($row['dateAdhesion']),
$row['numCategorie']);
}
return $tableau;
}
/**
* Trouve un adhérent selon son login unique.
* @param $login
* @return Adherent
* @throws Exception
*/
public static function getAdherentByLogin($login) : Adherent
{
// écriture de la requête
$requetePreparee = "SELECT * FROM Adherent WHERE login = :login;";
$req_prep = Database::pdo()->prepare($requetePreparee);
// le tableau des valeurs
$valeurs = array("login" => $login);
$response = null;
try {
// envoi de la requête
$req_prep->execute($valeurs);
// traitement de la réponse
$req_prep->setFetchmode(PDO::FETCH_ASSOC);
// récupération de l'auteur
$row = $req_prep->fetch();
$response = new Adherent($row['login'],
$row['mdp'],
$row['nomAdherent'],
$row['prenomAdherent'],
$row['email'],
new DateTime($row['dateAdhesion']),
$row['numCategorie']);
} catch (PDOException $e) {
echo $e->getMessage();
}
return $response;
}
/**
* @return string
*/
public function afficher(): string
{
return "<p>" . $this->prenomAdherent . " " . $this->nomAdherent . " a rejoint le "
.$this->dateAdhesion->format('Y-m-d H:i:s');
}
}
\ No newline at end of file
<?php
/**
*
*/
class Categorie
{
private int $numCategorie;
private string $libelle;
private int $nbLivresAutorises;
/**
* @param int $numCategorie
* @param string $libelle
* @param int $nbLivresAutorises
*/
public function __construct(int $numCategorie, string $libelle, int $nbLivresAutorises)
{
$this->numCategorie = $numCategorie;
$this->libelle = $libelle;
$this->nbLivresAutorises = $nbLivresAutorises;
}
/**
* @return int
*/
public function getNumCategorie(): int
{
return $this->numCategorie;
}
/**
* @param int $numCategorie
*/
public function setNumCategorie(int $numCategorie): void
{
$this->numCategorie = $numCategorie;
}
/**
* @return string
*/
public function getLibelle(): string
{
return $this->libelle;
}
/**
* @param string $libelle
*/
public function setLibelle(string $libelle): void
{
$this->libelle = $libelle;
}
/**
* @return int
*/
public function getNbLivresAutorises(): int
{
return $this->nbLivresAutorises;
}
/**
* @param int $nbLivresAutorises
*/
public function setNbLivresAutorises(int $nbLivresAutorises): void
{
$this->nbLivresAutorises = $nbLivresAutorises;
}
/**
* @return void
*/
public function afficher(): void{
echo "<p>Le livre" . $this->libelle . " est autorisé " . $this->nbLivresAutorises . " fois.</p>";
}
}
\ No newline at end of file
<?php
class DateEmprunt
{
private int $numDateEmprunt;
private DateTime $dateEmrpunt;
/**
* @return int
*/
public function getNumDateEmprunt(): int
{
return $this->numDateEmprunt;
}
/**
* @param int $numDateEmprunt
*/
public function setNumDateEmprunt(int $numDateEmprunt): void
{
$this->numDateEmprunt = $numDateEmprunt;
}
/**
* @return DateTime
*/
public function getDateEmrpunt(): DateTime
{
return $this->dateEmrpunt;
}
/**
* @param DateTime $dateEmrpunt
*/
public function setDateEmrpunt(DateTime $dateEmrpunt): void
{
$this->dateEmrpunt = $dateEmrpunt;
}
}
\ No newline at end of file
<?php
class Genre
{
private int $numGenre;
private string $intitule;
/**
* @param int $numGenre
* @param string $intitule
*/
public function __construct(int $numGenre, string $intitule)
{
$this->numGenre = $numGenre;
$this->intitule = $intitule;
}
/**
* @return int
*/
public function getNumGenre(): int
{
return $this->numGenre;
}
/**
* @param int $numGenre
*/
public function setNumGenre(int $numGenre): void
{
$this->numGenre = $numGenre;
}
/**
* @return string
*/
public function getIntitule(): string
{
return $this->intitule;
}
/**
* @param string $intitule
*/
public function setIntitule(string $intitule): void
{
$this->intitule = $intitule;
}
}
\ No newline at end of file
<?php
require_once "Objet.php";
class Livre extends Objet
{
protected int $numLivre;
protected string $titre;
protected int $anneeParution;
protected int $numGenre;
/**
* @param int $numLivre
* @param string $titre
* @param int $anneeParution
* @param int $numGenre
*/
public function __construct(int $numLivre, string $titre, int $anneeParution, int $numGenre)
{
$this->numLivre = $numLivre;
$this->titre = $titre;
$this->anneeParution = $anneeParution;
$this->numGenre = $numGenre;
}
/**
* @return array
*/
public static function getAllLivres(): array
{
$query = Database::pdo()->query("SELECT * FROM Livre");
$query->setFetchmode(PDO::FETCH_ASSOC);
$queryResult = $query->fetchAll();
$tableau = array();
foreach ($queryResult as $row) {
$tableau[] = new Livre($row['numLivre'],
$row['titre'],
$row['anneeParution'],
$row['numGenre']);
}
return $tableau;
}
/**
* @param int $numLivre
* @return Livre
*/
public static function getLivreByNumLivre(int $numLivre): Livre
{
$query = Database::pdo()->prepare("SELECT * FROM Livre WHERE numLivre = :numLivre");
$response = null;
try {
// envoi de la requête
$query->execute(['numLivre' => $numLivre]);
$query->setFetchmode(PDO::FETCH_ASSOC);
$queryResult = $query->fetch();
$response = new Livre($queryResult['numLivre'],
$queryResult['titre'],
$queryResult['anneeParution'],
$queryResult['numGenre']);
} catch (PDOException $e) {
echo $e->getMessage();
}
return $response;
}
/**
* @return string
*/
public function afficher(): string
{
return "<p>" . $this->titre . " est paru en "
. $this->anneeParution;
}
}
\ No newline at end of file
<?php
class Nationalite
{
private int $numNationalite;
private string $pays;
private string $abrege;
/**
* @param int $numNationalite
* @param string $pays
* @param string $abrege
*/
public function __construct(int $numNationalite, string $pays, string $abrege)
{
$this->numNationalite = $numNationalite;
$this->pays = $pays;
$this->abrege = $abrege;
}
/**
* @return int
*/
public function getNumNationalite(): int
{
return $this->numNationalite;
}
/**
* @param int $numNationalite
*/
public function setNumNationalite(int $numNationalite): void
{
$this->numNationalite = $numNationalite;
}
}
\ No newline at end of file
File added
<?php
/**
* Permet de récupérer un paramètre du fichier config.php
* @param string $configIndex
* @return mixed
*/
function config(string $configIndex) : mixed
{
$configPath = getcwd() . "/config.php";
if(file_exists($configPath)){
$config = include $configPath;
$indexes = explode(".", $configIndex);
$tempConf = $config;
foreach ($indexes as $index){
if(array_key_exists($index, $tempConf)){
$tempConf = $tempConf[$index];
}else{
return null;
}
}
return $tempConf;
} else {
return null;
}
}
\ No newline at end of file
File moved
File moved
......@@ -3,29 +3,25 @@
class Database
{
// les attributs static caractéristiques de la connexion
static private $hostname = 'localhost';
static private $database = 'iut-dev';
static private $login = 'iut-dev-user';
static private $password = 'p73i74KAV8lami2iyIpehE5ozic8GA';
static private $tabUTF8 = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
static private array $tabUTF8 = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
// l'attribut static qui matérialisera la connexion
static private $pdo;
static private PDO $pdo;
// le getter public de cet attribut
static public function pdo()
static public function pdo(): PDO
{
return self::$pdo;
}
// la fonction static de connexion qui initialise $pdo et lance la tentative de connexion
static public function connect()
static public function connect(): void
{
$hostname = config("DB_HOST");
$database = config("DB_NAME");
$login = config("DB_LOGIN");
$password = config("DB_PASSWORD");
$hostname = config("database.host");
$database = config("database.name");
$login = config("database.login");
$password = config("database.password");
$t = self::$tabUTF8;
try {
self::$pdo = new PDO("mysql:host=$hostname;dbname=$database", $login, $password, $t);
......@@ -36,4 +32,4 @@ class Database
}
}
?>
......@@ -29,7 +29,6 @@ class View
/**
* S'occuper de remplir la template
* @param string $viewPath
* @param array $args
* @return string
*/
public static function parse(string $viewPath): string
......
<?php
return ['database' => [
'host' => 'localhost',
'name' => 'iut-dev',
'login' => 'iut-dev-user',
'password' => 'p73i74KAV8lami2iyIpehE5ozic8GA'
]];
\ No newline at end of file
......@@ -16,8 +16,8 @@ class ControleurAdherent extends ControleurObjet
$objects = [];
foreach ($adherents as $adherent){
$object['title'] = $adherent->prenomAdherent . " " . $adherent->nomAdherent;
$object['desc'] = "ID: {$adherent->login}<br>Date d'adhésion: {$adherent->dateAdhesion->format("Y-m-d")}";
$object['url'] = "index.php?action=lireAdherent&login={$adherent->login}";
$object['desc'] = "ID: $adherent->login<br>Date d'adhésion: {$adherent->dateAdhesion->format("Y-m-d")}";
$object['url'] = "index.php?action=lireAdherent&login=$adherent->login";
$objects[] = $object;
}
return view('data-grid', [
......@@ -39,7 +39,7 @@ class ControleurAdherent extends ControleurObjet
}
$adherent = Adherent::getAdherentByLogin($_GET["login"]);
return view('simple-data', [
'pageTitle' => 'Information du livre',
'pageTitle' => "Information de l'adhérent",
'pageContent' => $adherent->afficher()
]);
}
......
......@@ -16,8 +16,8 @@ class ControleurAuteur extends ControleurObjet
$objects = [];
foreach ($auteurs as $auteur){
$object['title'] = $auteur->prenom . " " . $auteur->nom;
$object['desc'] = "ID: {$auteur->numAuteur}<br>Date de naissance: {$auteur->anneeNaissance}";
$object['url'] = "index.php?action=lireAuteur&numAuteur={$auteur->numAuteur}";
$object['desc'] = "ID: $auteur->numAuteur<br>Date de naissance: $auteur->anneeNaissance";
$object['url'] = "index.php?action=lireAuteur&numAuteur=$auteur->numAuteur";
$objects[] = $object;
}
return view('data-grid', [
......@@ -38,7 +38,7 @@ class ControleurAuteur extends ControleurObjet
}
$auteur = Auteur::getAuteurByNum($_GET["numAuteur"]);
return view('simple-data', [
'pageTitle' => 'Information du livre',
'pageTitle' => "Information de l'auteur",
'pageContent' => $auteur->afficher()
]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment