소스 검색

Finished model with error cases and a few comments

Brendan Abolivier 9 년 전
부모
커밋
53a2a35542
4개의 변경된 파일50개의 추가작업 그리고 8개의 파일을 삭제
  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 파일 보기

@@ -36,6 +36,13 @@ class Connector {
36 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 46
     function Select($fields, $tables, $options = array()) {
40 47
         $request = "SELECT $fields FROM $tables ";
41 48
         $arrayVerif = array();
@@ -44,7 +51,7 @@ class Connector {
44 51
                 $whereClause = " $upName ";
45 52
                 foreach($value as $array) {
46 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 56
                     if(sizeof($array) == 3) {
50 57
                         $whereClause .= $array[0]." ".$array[1]." ? AND ";
@@ -57,7 +64,7 @@ class Connector {
57 64
                 $request .= substr($whereClause, 0, -5);
58 65
             } else if(($upName = strtoupper($name)) == "ORDER BY") {
59 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 70
                 $request .= " ".$upName." ".implode(" ", $value);
@@ -70,10 +77,10 @@ class Connector {
70 77
                     // nombre de champs
71 78
                     $request .= " $upName ".$value[0].",".$value[1];
72 79
                 } else {
73
-                    throw new Exception("wrong_arg_numbr_limit");
80
+                    throw new InvalidArgumentException("wrong_arg_numbr_limit");
74 81
                 }
75 82
             } else {
76
-                throw new Exception("unknown_arg");
83
+                throw new InvalidArgumentException("unknown_arg");
77 84
             }
78 85
         }
79 86
 
@@ -86,6 +93,10 @@ class Connector {
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 100
     function Insert($table, $values) {
90 101
         $request = "INSERT INTO $table(";
91 102
         $valeurs = "VALUES(";
@@ -103,6 +114,11 @@ class Connector {
103 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 122
     function Update($table, $update) {
107 123
         $request = "UPDATE $table SET ";
108 124
         $arrayVerif = array();
@@ -121,6 +137,10 @@ class Connector {
121 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 144
     function Delete($table, $where = array()) {
125 145
         $request = "DELETE FROM $table";
126 146
         $arrayVerif = array();

+ 11
- 1
models/data.class.php 파일 보기

@@ -25,7 +25,7 @@ class Data
25 25
 
26 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 31
         // Chargement des informations
@@ -177,6 +177,11 @@ class Data
177 177
             )
178 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 185
         $attrs = get_object_vars($this);
181 186
         $toUpdate = array();
182 187
 
@@ -197,6 +202,11 @@ class Data
197 202
         $bdd = new Connector();
198 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 210
         $csv = "";
201 211
 
202 212
         // Head line

+ 9
- 2
models/file.class.php 파일 보기

@@ -21,7 +21,7 @@ class File
21 21
 
22 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 27
         $this->id = $document["id"];
@@ -33,6 +33,13 @@ class File
33 33
 
34 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 43
         $bdd = new Connector();
37 44
         $bdd->Insert("document", array(
38 45
             "id" => $document["id"],
@@ -63,7 +70,7 @@ class File
63 70
 
64 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 76
         // Change promo in both object and BDD

+ 6
- 1
models/promo.class.php 파일 보기

@@ -18,7 +18,7 @@ class Promo
18 18
 
19 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 24
         $this->id_promo = $promo["id_promo"];
@@ -73,6 +73,11 @@ class Promo
73 73
             )
74 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 81
         $attrs = get_object_vars($this);
77 82
         $toUpdate = array();
78 83