Browse Source

Finished model with error cases and a few comments

Brendan Abolivier 9 years ago
parent
commit
53a2a35542
4 changed files with 50 additions and 8 deletions
  1. 24
    4
      models/connector.class.php
  2. 11
    1
      models/data.class.php
  3. 9
    2
      models/file.class.php
  4. 6
    1
      models/promo.class.php

+ 24
- 4
models/connector.class.php View File

36
         $this->bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
36
         $this->bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
37
     }
37
     }
38
 
38
 
39
+    /**
40
+     * @param string $fields Fields to select
41
+     * @param string $tables Tables to select from
42
+     * @param mixed[] $options Array filled with the selection options, such as "WHERE", "ORDER BY" and "LIMIT"
43
+     * @return mixed[]|null Array of values if the request went OK, NULL else
44
+     * @throws InvalidArgumentException There was an error within the arguments array
45
+     */
39
     function Select($fields, $tables, $options = array()) {
46
     function Select($fields, $tables, $options = array()) {
40
         $request = "SELECT $fields FROM $tables ";
47
         $request = "SELECT $fields FROM $tables ";
41
         $arrayVerif = array();
48
         $arrayVerif = array();
44
                 $whereClause = " $upName ";
51
                 $whereClause = " $upName ";
45
                 foreach($value as $array) {
52
                 foreach($value as $array) {
46
                     if(sizeof($array) != 3 && sizeof($array) != 4) {
53
                     if(sizeof($array) != 3 && sizeof($array) != 4) {
47
-                        throw new Exception("wrong_arg_nmbr_where");
54
+                        throw new InvalidArgumentException("wrong_arg_nmbr_where");
48
                     }
55
                     }
49
                     if(sizeof($array) == 3) {
56
                     if(sizeof($array) == 3) {
50
                         $whereClause .= $array[0]." ".$array[1]." ? AND ";
57
                         $whereClause .= $array[0]." ".$array[1]." ? AND ";
57
                 $request .= substr($whereClause, 0, -5);
64
                 $request .= substr($whereClause, 0, -5);
58
             } else if(($upName = strtoupper($name)) == "ORDER BY") {
65
             } else if(($upName = strtoupper($name)) == "ORDER BY") {
59
                 if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
66
                 if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
60
-                    throw new Exception("wrong_arg_nmbr_order_by");
67
+                    throw new InvalidArgumentException("wrong_arg_nmbr_order_by");
61
                 }
68
                 }
62
 
69
 
63
                 $request .= " ".$upName." ".implode(" ", $value);
70
                 $request .= " ".$upName." ".implode(" ", $value);
70
                     // nombre de champs
77
                     // nombre de champs
71
                     $request .= " $upName ".$value[0].",".$value[1];
78
                     $request .= " $upName ".$value[0].",".$value[1];
72
                 } else {
79
                 } else {
73
-                    throw new Exception("wrong_arg_numbr_limit");
80
+                    throw new InvalidArgumentException("wrong_arg_numbr_limit");
74
                 }
81
                 }
75
             } else {
82
             } else {
76
-                throw new Exception("unknown_arg");
83
+                throw new InvalidArgumentException("unknown_arg");
77
             }
84
             }
78
         }
85
         }
79
 
86
 
86
         }
93
         }
87
     }
94
     }
88
 
95
 
96
+    /**
97
+     * @param string $table Name of the table where the insertion takes place
98
+     * @param string[] $values Array of values to insert in the database
99
+     */
89
     function Insert($table, $values) {
100
     function Insert($table, $values) {
90
         $request = "INSERT INTO $table(";
101
         $request = "INSERT INTO $table(";
91
         $valeurs = "VALUES(";
102
         $valeurs = "VALUES(";
103
         $stmt->execute($arrayVerif);
114
         $stmt->execute($arrayVerif);
104
     }
115
     }
105
 
116
 
117
+    /**
118
+     * @param string $table Name of the table where the update takes place
119
+     * @param mixed[] $update Array of fields to update with new values, with a "set"
120
+     *                          row filled with values, and a "where" row with conditions
121
+     */
106
     function Update($table, $update) {
122
     function Update($table, $update) {
107
         $request = "UPDATE $table SET ";
123
         $request = "UPDATE $table SET ";
108
         $arrayVerif = array();
124
         $arrayVerif = array();
121
         $stmt->execute($arrayVerif);
137
         $stmt->execute($arrayVerif);
122
     }
138
     }
123
 
139
 
140
+    /**
141
+     * @param string $table Name of the table where the deletion takes place
142
+     * @param mixed[] $where Array of conditions to select the right row(s) to delete
143
+     */
124
     function Delete($table, $where = array()) {
144
     function Delete($table, $where = array()) {
125
         $request = "DELETE FROM $table";
145
         $request = "DELETE FROM $table";
126
         $arrayVerif = array();
146
         $arrayVerif = array();

+ 11
- 1
models/data.class.php View File

25
 
25
 
26
         if($data == NULL)
26
         if($data == NULL)
27
         {
27
         {
28
-            throw new Exception("Les données n'existent pas");
28
+            throw new LengthException("Les données n'existent pas");
29
         }
29
         }
30
 
30
 
31
         // Chargement des informations
31
         // Chargement des informations
177
             )
177
             )
178
         ))[0];
178
         ))[0];
179
 
179
 
180
+        if(!$data)
181
+        {
182
+            throw new UnexpectedValueException("Les données n'existent plus en base de données");
183
+        }
184
+
180
         $attrs = get_object_vars($this);
185
         $attrs = get_object_vars($this);
181
         $toUpdate = array();
186
         $toUpdate = array();
182
 
187
 
197
         $bdd = new Connector();
202
         $bdd = new Connector();
198
         $data = $bdd->Select("*", "data");
203
         $data = $bdd->Select("*", "data");
199
 
204
 
205
+        if(!$data)
206
+        {
207
+            throw new LengthException("Aucune donnée présente en base de données");
208
+        }
209
+
200
         $csv = "";
210
         $csv = "";
201
 
211
 
202
         // Head line
212
         // Head line

+ 9
- 2
models/file.class.php View File

21
 
21
 
22
         if(!$document)
22
         if(!$document)
23
         {
23
         {
24
-            throw new Exception("Le fichier n'existe pas");
24
+            throw new LengthException("Le fichier n'existe pas");
25
         }
25
         }
26
 
26
 
27
         $this->id = $document["id"];
27
         $this->id = $document["id"];
33
 
33
 
34
     public static function addDocument($document)
34
     public static function addDocument($document)
35
     {
35
     {
36
+        foreach($document as $key=>$value)
37
+        {
38
+            if(empty($value) && $key != "promo" && $key != "id")
39
+            {
40
+                throw InvalidArgumentException("La colonne `".$key."` doit être définie");
41
+            }
42
+        }
36
         $bdd = new Connector();
43
         $bdd = new Connector();
37
         $bdd->Insert("document", array(
44
         $bdd->Insert("document", array(
38
             "id" => $document["id"],
45
             "id" => $document["id"],
63
 
70
 
64
         if(!$promo)
71
         if(!$promo)
65
         {
72
         {
66
-            throw new Exception("La promo n'existe pas");
73
+            throw new LengthException("La promo n'existe pas");
67
         }
74
         }
68
 
75
 
69
         // Change promo in both object and BDD
76
         // Change promo in both object and BDD

+ 6
- 1
models/promo.class.php View File

18
 
18
 
19
         if($promo == NULL)
19
         if($promo == NULL)
20
         {
20
         {
21
-            throw new Exception("La promo n'existe pas");
21
+            throw new LengthException("La promo n'existe pas");
22
         }
22
         }
23
 
23
 
24
         $this->id_promo = $promo["id_promo"];
24
         $this->id_promo = $promo["id_promo"];
73
             )
73
             )
74
         ))[0];
74
         ))[0];
75
 
75
 
76
+        if(!$promo)
77
+        {
78
+            throw new UnexpectedValueException("La promo n'existe plus en base de données");
79
+        }
80
+
76
         $attrs = get_object_vars($this);
81
         $attrs = get_object_vars($this);
77
         $toUpdate = array();
82
         $toUpdate = array();
78
 
83