From b393b0fee9020c2b02d76f2937d93d411e01c005 Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Fri, 28 Oct 2022 11:33:33 +0200
Subject: [PATCH] Fin de TP5 ex2 mais pas vraiment suivi.

---
 TP5/controller/ControleurAdherent.php |  1 +
 TP5/controller/ControleurAuteur.php   |  1 +
 TP5/controller/ControleurLivre.php    |  3 +-
 TP5/controller/ControleurObjet.php    | 11 ++++++-
 TP5/index.php                         | 47 ++++++++++++++++++++-------
 5 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/TP5/controller/ControleurAdherent.php b/TP5/controller/ControleurAdherent.php
index 395bd29..7b97446 100644
--- a/TP5/controller/ControleurAdherent.php
+++ b/TP5/controller/ControleurAdherent.php
@@ -4,6 +4,7 @@ require_once("models/Adherent.php");
 
 class ControleurAdherent extends ControleurObjet
 {
+    protected static string $object = "Adherent";
     /**
      * Charge la page de la liste des adhérents.
      * @return string
diff --git a/TP5/controller/ControleurAuteur.php b/TP5/controller/ControleurAuteur.php
index 93b7bd1..37c589f 100644
--- a/TP5/controller/ControleurAuteur.php
+++ b/TP5/controller/ControleurAuteur.php
@@ -4,6 +4,7 @@ require_once("models/Auteur.php");
 
 class ControleurAuteur extends ControleurObjet
 {
+    protected static string $object = "Auteur";
     /**
      * Charge la page de la liste des auteurs.
      * @return string
diff --git a/TP5/controller/ControleurLivre.php b/TP5/controller/ControleurLivre.php
index dfb1633..0f0f651 100644
--- a/TP5/controller/ControleurLivre.php
+++ b/TP5/controller/ControleurLivre.php
@@ -4,6 +4,7 @@ require_once("models/Livre.php");
 
 class ControleurLivre extends ControleurObjet
 {
+    protected static string $object = "Livre";
     /**
      * Charge la page de la liste des adhérents.
      * @return string
@@ -40,7 +41,7 @@ class ControleurLivre extends ControleurObjet
         $livre = Livre::getLivreByNumLivre($_GET["numLivre"]);
 
         return view('simple-data', [
-            'pageTitle' => 'Information du libre',
+            'pageTitle' => 'Information du livre',
             'pageContent' => $livre->afficher()
         ]);
     }
diff --git a/TP5/controller/ControleurObjet.php b/TP5/controller/ControleurObjet.php
index 2ed8809..04f3448 100644
--- a/TP5/controller/ControleurObjet.php
+++ b/TP5/controller/ControleurObjet.php
@@ -1,6 +1,15 @@
 <?php
 
+/**
+ * Désolé mais le TP5 est une mauvaise idée de bout en bout.
+ * Je me suis arrêté à l'ex 2 car je ne vois pas comment on pourrait aisément rassembler tous les contrôleurs ici
+ * étant donné que le contenu diffère légèrement selon les modèles.
+ */
 class ControleurObjet
 {
-
+    public static function lireObjets() : string
+    {
+        $titre = "Listes des " . strtolower(static::$object) . "s";
+        return $titre;
+    }
 }
\ No newline at end of file
diff --git a/TP5/index.php b/TP5/index.php
index 17c98ca..47a83c3 100644
--- a/TP5/index.php
+++ b/TP5/index.php
@@ -17,16 +17,41 @@ Database::connect();
 if (empty($_GET["action"])) {
     echo ControleurAuteur::lireAuteurs();
 } else {
-    if (in_array($_GET["action"], get_class_methods('ControleurAuteur'))) {
-        $action = $_GET["action"];
-        echo ControleurAuteur::$action();
-    } elseif (in_array($_GET["action"], get_class_methods('ControleurAdherent'))) {
-        $action = $_GET["action"];
-        echo ControleurAdherent::$action();
-    } elseif (in_array($_GET["action"], get_class_methods('ControleurLivre'))) {
-        $action = $_GET["action"];
-        echo ControleurLivre::$action();
+    // Test TP 5
+    if ($_GET["action"] === "lireObjets" && !empty($_GET["objet"])) {
+        switch ($_GET["objet"]){
+            case 'Auteur':
+                echo ControleurAuteur::lireObjets();
+                break;
+            default:
+                echo ControleurAuteur::lireObjets();
+                break;
+        }
     }
-}
+    // Fin test TP5
 
-?>
+    // C'est plus long que get_class_methods mais c'est moins sujet à problèmes.
+    switch ($_GET["action"]){
+        case "lireAuteur":
+            echo ControleurAuteur::lireAuteur();
+            break;
+        case "lireAuteurs":
+            echo ControleurAuteur::lireAuteurs();
+            break;
+        case "lireAdherent":
+            echo ControleurAdherent::lireAdherent();
+            break;
+        case "lireAdherents":
+            echo ControleurAdherent::lireAdherents();
+            break;
+        case "lireLivre":
+            echo ControleurLivre::lireLivre();
+            break;
+        case "lireLivres":
+            echo ControleurLivre::lireLivres();
+            break;
+        default:
+            echo ControleurAuteur::lireAuteur();
+            break;
+    }
+}
-- 
GitLab