Completed
Push — test ( ebee65...cfa8af )
by Temitope
02:52
created
src/Database/DatabaseHandler.php 3 patches
Doc Comments   +6 added lines, -1 removed lines patch added patch discarded remove patch
@@ -17,6 +17,7 @@  discard block
 block discarded – undo
17 17
 
18 18
     /**
19 19
      * This is a constructor; a default method  that will be called automatically during class instantiation.
20
+     * @param string|false $modelClassName
20 21
      */
21 22
     public function __construct($modelClassName, $dbConn = null)
22 23
     {
@@ -33,6 +34,7 @@  discard block
 block discarded – undo
33 34
      * This method create a record and store it in a table row.
34 35
      *
35 36
      * @params associative array, string tablename
37
+     * @param string|false $tableName
36 38
      *
37 39
      * @return bool true or false
38 40
      */
@@ -92,6 +94,7 @@  discard block
 block discarded – undo
92 94
      * This method updates any table by supplying 3 parameter.
93 95
      *
94 96
      * @params: $updateParams, $tableName, $associative1DArray
97
+     * @param string|false $tableName
95 98
      *
96 99
      * @return bool true or false
97 100
      */
@@ -138,6 +141,7 @@  discard block
 block discarded – undo
138 141
     * This method retrieves record from a table.
139 142
     *
140 143
     * @params int id, string tableName
144
+    * @param string|false $tableName
141 145
     *
142 146
     * @return array
143 147
     */
@@ -169,6 +173,7 @@  discard block
 block discarded – undo
169 173
    * This method deletes a record  from a table row.
170 174
    *
171 175
    * @params int id, string tableName
176
+   * @param string|false $tableName
172 177
    *
173 178
    * @return bool true or false
174 179
    */
@@ -213,7 +218,7 @@  discard block
 block discarded – undo
213 218
   /**
214 219
    * This method returns sql query.
215 220
    *
216
-   * @param $sql
221
+   * @param string $sql
217 222
    *
218 223
    * @return string
219 224
    */
Please login to merge, or discard this patch.
Indentation   +177 added lines, -177 removed lines patch added patch discarded remove patch
@@ -10,129 +10,129 @@  discard block
 block discarded – undo
10 10
 
11 11
 class DatabaseHandler
12 12
 {
13
-    private $tableFields;
14
-    private $dbHelperInstance;
15
-    private $dbConnection;
16
-    private $model;
17
-
18
-    /**
19
-     * This is a constructor; a default method  that will be called automatically during class instantiation.
20
-     */
21
-    public function __construct($modelClassName, $dbConn = null)
22
-    {
23
-        if (is_null($dbConn)) {
24
-            $this->dbConnection = new DatabaseConnection();
25
-        } else {
26
-            $this->dbConnection = $dbConn;
27
-        }
28
-
29
-        $this->model = $modelClassName;
30
-    }
31
-
32
-    /**
33
-     * This method create a record and store it in a table row.
34
-     *
35
-     * @params associative array, string tablename
36
-     *
37
-     * @return bool true or false
38
-     */
39
-    public function create($associative1DArray, $tableName, $dbConn = null)
40
-    {
41
-        $tableFields = $this->getColumnNames($this->model, $this->dbConnection);
42
-
43
-        $unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray);
44
-
45
-        if (count($unexpectedFields) > 0) {
46
-            throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field');
47
-        }
48
-
49
-        unset($associative1DArray[0]);
50
-
51
-        if (is_null($dbConn)) {
52
-            $dbConn = $this->dbConnection;
53
-        }
54
-
55
-        return $this->insertRecord($dbConn, $tableName, $associative1DArray);
56
-    }
57
-
58
-    /**
59
-     * This method runs the insertion query.
60
-     *
61
-     * @param  $dbConn
62
-     * @param  $tableName
63
-     * @param  $associative1DArray
64
-     *
65
-     * @return bool true
66
-     */
67
-    private function insertRecord($dbConn, $tableName, $associative1DArray)
68
-    {
69
-        $insertQuery = 'INSERT INTO '.$tableName;
70
-
71
-        $TableValues = implode(',', array_keys($associative1DArray));
72
-
73
-        foreach ($associative1DArray as $field => $value) {
74
-            $FormValues[] = "'".trim(addslashes($value))."'";
75
-        }
76
-
77
-        $splittedTableValues = implode(',', $FormValues);
78
-
79
-        $insertQuery .= ' ('.$TableValues.')';
80
-        $insertQuery .= ' VALUES ('.$splittedTableValues.')';
81
-
82
-        $executeQuery = $dbConn->exec($insertQuery);
83
-
84
-        if ($executeQuery) {
85
-            return true;
86
-        }
87
-
88
-        return false;
89
-    }
90
-
91
-    /**
92
-     * This method updates any table by supplying 3 parameter.
93
-     *
94
-     * @params: $updateParams, $tableName, $associative1DArray
95
-     *
96
-     * @return bool true or false
97
-     */
98
-    public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null)
99
-    {
100
-        $sql = '';
101
-
102
-        if (is_null($dbConn)) {
103
-            $dbConn = $this->dbConnection;
104
-        }
105
-
106
-        $updateSql = "UPDATE `$tableName` SET ";
107
-
108
-        unset($associative1DArray['id']);
109
-
110
-        $unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $dbConn), $associative1DArray);
111
-
112
-        if (count($unexpectedFields) > 0) {
113
-            throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field');
114
-        }
13
+	private $tableFields;
14
+	private $dbHelperInstance;
15
+	private $dbConnection;
16
+	private $model;
17
+
18
+	/**
19
+	 * This is a constructor; a default method  that will be called automatically during class instantiation.
20
+	 */
21
+	public function __construct($modelClassName, $dbConn = null)
22
+	{
23
+		if (is_null($dbConn)) {
24
+			$this->dbConnection = new DatabaseConnection();
25
+		} else {
26
+			$this->dbConnection = $dbConn;
27
+		}
28
+
29
+		$this->model = $modelClassName;
30
+	}
31
+
32
+	/**
33
+	 * This method create a record and store it in a table row.
34
+	 *
35
+	 * @params associative array, string tablename
36
+	 *
37
+	 * @return bool true or false
38
+	 */
39
+	public function create($associative1DArray, $tableName, $dbConn = null)
40
+	{
41
+		$tableFields = $this->getColumnNames($this->model, $this->dbConnection);
42
+
43
+		$unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray);
44
+
45
+		if (count($unexpectedFields) > 0) {
46
+			throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field');
47
+		}
48
+
49
+		unset($associative1DArray[0]);
50
+
51
+		if (is_null($dbConn)) {
52
+			$dbConn = $this->dbConnection;
53
+		}
54
+
55
+		return $this->insertRecord($dbConn, $tableName, $associative1DArray);
56
+	}
57
+
58
+	/**
59
+	 * This method runs the insertion query.
60
+	 *
61
+	 * @param  $dbConn
62
+	 * @param  $tableName
63
+	 * @param  $associative1DArray
64
+	 *
65
+	 * @return bool true
66
+	 */
67
+	private function insertRecord($dbConn, $tableName, $associative1DArray)
68
+	{
69
+		$insertQuery = 'INSERT INTO '.$tableName;
70
+
71
+		$TableValues = implode(',', array_keys($associative1DArray));
72
+
73
+		foreach ($associative1DArray as $field => $value) {
74
+			$FormValues[] = "'".trim(addslashes($value))."'";
75
+		}
76
+
77
+		$splittedTableValues = implode(',', $FormValues);
78
+
79
+		$insertQuery .= ' ('.$TableValues.')';
80
+		$insertQuery .= ' VALUES ('.$splittedTableValues.')';
81
+
82
+		$executeQuery = $dbConn->exec($insertQuery);
83
+
84
+		if ($executeQuery) {
85
+			return true;
86
+		}
87
+
88
+		return false;
89
+	}
90
+
91
+	/**
92
+	 * This method updates any table by supplying 3 parameter.
93
+	 *
94
+	 * @params: $updateParams, $tableName, $associative1DArray
95
+	 *
96
+	 * @return bool true or false
97
+	 */
98
+	public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null)
99
+	{
100
+		$sql = '';
101
+
102
+		if (is_null($dbConn)) {
103
+			$dbConn = $this->dbConnection;
104
+		}
105
+
106
+		$updateSql = "UPDATE `$tableName` SET ";
107
+
108
+		unset($associative1DArray['id']);
109
+
110
+		$unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $dbConn), $associative1DArray);
111
+
112
+		if (count($unexpectedFields) > 0) {
113
+			throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field');
114
+		}
115 115
 
116
-        foreach ($associative1DArray as $field => $value) {
117
-            $sql .= "`$field` = '$value'".',';
118
-        }
116
+		foreach ($associative1DArray as $field => $value) {
117
+			$sql .= "`$field` = '$value'".',';
118
+		}
119 119
 
120
-        $updateSql .= $this->prepareUpdateQuery($sql);
120
+		$updateSql .= $this->prepareUpdateQuery($sql);
121 121
 
122
-        foreach ($updateParams as $key => $val) {
123
-            $updateSql .= " WHERE $key = $val";
124
-        }
122
+		foreach ($updateParams as $key => $val) {
123
+			$updateSql .= " WHERE $key = $val";
124
+		}
125 125
 
126
-        $stmt = $dbConn->prepare($updateSql);
126
+		$stmt = $dbConn->prepare($updateSql);
127 127
 
128
-        $boolResponse = $stmt->execute();
128
+		$boolResponse = $stmt->execute();
129 129
 
130
-        if ($boolResponse) {
131
-            return true;
132
-        }
130
+		if ($boolResponse) {
131
+			return true;
132
+		}
133 133
 
134
-        return false;
135
-    }
134
+		return false;
135
+	}
136 136
 
137 137
    /**
138 138
     * This method retrieves record from a table.
@@ -143,26 +143,26 @@  discard block
 block discarded – undo
143 143
     */
144 144
    public static function read($id, $tableName, $dbConn = null)
145 145
    {
146
-       $tableData = [];
146
+	   $tableData = [];
147 147
 
148
-       if (is_null($dbConn)) {
149
-           $dbConn = new DatabaseConnection();
150
-       }
148
+	   if (is_null($dbConn)) {
149
+		   $dbConn = new DatabaseConnection();
150
+	   }
151 151
 
152
-       $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
152
+	   $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
153 153
 
154
-       $stmt = $dbConn->prepare($sql);
155
-       $stmt->bindValue(':table', $tableName);
156
-       $stmt->bindValue(':id', $id);
157
-       $stmt->execute();
154
+	   $stmt = $dbConn->prepare($sql);
155
+	   $stmt->bindValue(':table', $tableName);
156
+	   $stmt->bindValue(':id', $id);
157
+	   $stmt->execute();
158 158
 
159
-       $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
159
+	   $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
160 160
 
161
-       foreach ($results as $result) {
162
-           array_push($tableData, $result);
163
-       }
161
+	   foreach ($results as $result) {
162
+		   array_push($tableData, $result);
163
+	   }
164 164
 
165
-       return $tableData;
165
+	   return $tableData;
166 166
    }
167 167
 
168 168
   /**
@@ -174,19 +174,19 @@  discard block
 block discarded – undo
174 174
    */
175 175
   public static function delete($id, $tableName, $dbConn = null)
176 176
   {
177
-      if (is_null($dbConn)) {
178
-          $dbConn = new DatabaseConnection();
179
-      }
177
+	  if (is_null($dbConn)) {
178
+		  $dbConn = new DatabaseConnection();
179
+	  }
180 180
 
181
-      $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
181
+	  $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
182 182
 
183
-      $boolResponse = $dbConn->exec($sql);
183
+	  $boolResponse = $dbConn->exec($sql);
184 184
 
185
-      if ($boolResponse) {
186
-          return true;
187
-      }
185
+	  if ($boolResponse) {
186
+		  return true;
187
+	  }
188 188
 
189
-      throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record');
189
+	  throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record');
190 190
   }
191 191
 
192 192
   /**
@@ -199,15 +199,15 @@  discard block
 block discarded – undo
199 199
    */
200 200
   public static function filterClassAttributes(array $tableColumn, array $userSetterArray)
201 201
   {
202
-      $unexpectedFields = [];
202
+	  $unexpectedFields = [];
203 203
 
204
-      foreach ($userSetterArray as $key => $val) {
205
-          if (! in_array($key, $tableColumn)) {
206
-              $unexpectedFields[] = $key;
207
-          }
208
-      }
204
+	  foreach ($userSetterArray as $key => $val) {
205
+		  if (! in_array($key, $tableColumn)) {
206
+			  $unexpectedFields[] = $key;
207
+		  }
208
+	  }
209 209
 
210
-      return $unexpectedFields;
210
+	  return $unexpectedFields;
211 211
   }
212 212
 
213 213
   /**
@@ -219,13 +219,13 @@  discard block
 block discarded – undo
219 219
    */
220 220
   public function prepareUpdateQuery($sql)
221 221
   {
222
-      $splittedQuery = explode(',', $sql);
222
+	  $splittedQuery = explode(',', $sql);
223 223
 
224
-      array_pop($splittedQuery);
224
+	  array_pop($splittedQuery);
225 225
 
226
-      $mergeData = implode(',', $splittedQuery);
226
+	  $mergeData = implode(',', $splittedQuery);
227 227
 
228
-      return $mergeData;
228
+	  return $mergeData;
229 229
   }
230 230
 
231 231
   /**
@@ -239,26 +239,26 @@  discard block
 block discarded – undo
239 239
    */
240 240
   public function findAndWhere($params, $tableName, $dbConn = null)
241 241
   {
242
-      if (is_null($dbConn)) {
243
-          $dbConn = $this->dbConnection;
244
-      }
242
+	  if (is_null($dbConn)) {
243
+		  $dbConn = $this->dbConnection;
244
+	  }
245 245
 
246
-      if (is_array($params) && !empty($params)) {
247
-          $sql = 'SELECT * FROM '.$tableName;
246
+	  if (is_array($params) && !empty($params)) {
247
+		  $sql = 'SELECT * FROM '.$tableName;
248 248
 
249
-          foreach ($params as $key => $val) {
250
-              $sql .= " WHERE `$key` = '$val'";
251
-          }
249
+		  foreach ($params as $key => $val) {
250
+			  $sql .= " WHERE `$key` = '$val'";
251
+		  }
252 252
 
253
-          $statement = $dbConn->prepare($sql);
254
-          $statement->execute();
253
+		  $statement = $dbConn->prepare($sql);
254
+		  $statement->execute();
255 255
 
256
-          $returnedRowNumbers = $statement->rowCount();
256
+		  $returnedRowNumbers = $statement->rowCount();
257 257
 
258
-          return $returnedRowNumbers ? true : false;
259
-      }
258
+		  return $returnedRowNumbers ? true : false;
259
+	  }
260 260
 
261
-      throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array');
261
+	  throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array');
262 262
   }
263 263
 
264 264
   /**
@@ -271,24 +271,24 @@  discard block
 block discarded – undo
271 271
    */
272 272
   public function getColumnNames($table, $dbConn = null)
273 273
   {
274
-      $tableFields = [];
274
+	  $tableFields = [];
275 275
 
276
-      if (is_null($dbConn)) {
277
-          $dbConn = $this->dbConnection;
278
-      }
276
+	  if (is_null($dbConn)) {
277
+		  $dbConn = $this->dbConnection;
278
+	  }
279 279
 
280
-      $sql = 'SHOW COLUMNS FROM '.$table;
280
+	  $sql = 'SHOW COLUMNS FROM '.$table;
281 281
 
282
-      $stmt = $dbConn->prepare($sql);
283
-      $stmt->bindValue(':table', $table, PDO::PARAM_STR);
284
-      $stmt->execute();
282
+	  $stmt = $dbConn->prepare($sql);
283
+	  $stmt->bindValue(':table', $table, PDO::PARAM_STR);
284
+	  $stmt->execute();
285 285
 
286
-      $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
286
+	  $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
287 287
 
288
-      foreach ($results as $result) {
289
-          array_push($tableFields, $result['Field']);
290
-      }
288
+	  foreach ($results as $result) {
289
+		  array_push($tableFields, $result['Field']);
290
+	  }
291 291
 
292
-      return $tableFields;
292
+	  return $tableFields;
293 293
   }
294 294
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -202,7 +202,7 @@
 block discarded – undo
202 202
       $unexpectedFields = [];
203 203
 
204 204
       foreach ($userSetterArray as $key => $val) {
205
-          if (! in_array($key, $tableColumn)) {
205
+          if (!in_array($key, $tableColumn)) {
206 206
               $unexpectedFields[] = $key;
207 207
           }
208 208
       }
Please login to merge, or discard this patch.
src/Model/BaseModel.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -8,169 +8,169 @@  discard block
 block discarded – undo
8 8
 
9 9
 class BaseModel implements BaseModelInterface
10 10
 {
11
-    // Inject the inflector trait
12
-    use Inflector;
13
-
14
-    // Private variable that contains instance of database
15
-    protected $databaseModel;
16
-
17
-    // Class variable holding class name pluralized
18
-    protected $tableName;
19
-
20
-    // Properties will later contain key, value pairs from the magic setter, getter methods
21
-    protected $properties = [];
22
-
23
-    public function __construct()
24
-    {
25
-        $this->tableName = $this->getClassName();
26
-
27
-        $this->databaseModel = new DatabaseHandler($this->tableName);
28
-
29
-        $this->properties['id'] = 0;
30
-    }
31
-
32
-    /**
33
-     * The magic getter method.
34
-     *
35
-     * @params key
36
-     *
37
-     * @return array key
38
-     */
39
-    public function __get($key)
40
-    {
41
-        $this->properties[$key];
42
-    }
43
-
44
-    /**
45
-     * The magic setter method.
46
-     *
47
-     * @params property, key
48
-     *
49
-     * @return array associative array properties
50
-     */
51
-    public function __set($property, $value)
52
-    {
53
-        $this->properties[$property] = $value;
54
-    }
55
-
56
-    /**
57
-     * This method gets all the record from a particular table.
58
-     *
59
-     * @params void
60
-     *
61
-     * @throws NoRecordFoundException
62
-     *
63
-     * @return associative array
64
-     */
65
-    public static function getAll()
66
-    {
67
-        $allData = DatabaseHandler::read($id = false, self::getClassName());
68
-
69
-        if (count($allData) > 0) {
70
-            return $allData;
71
-        }
72
-
73
-        throw NoRecordFoundException::create('There is no record to display');
74
-    }
75
-
76
-    /**
77
-     * This method create or update record in a database table.
78
-     *
79
-     * @params void
80
-     *
81
-     * @throws EmptyArrayException
82
-     * @throws NoRecordInsertionException
83
-     * @throws NoRecordUpdateException
84
-     *
85
-     * @return bool true or false;
86
-     */
87
-    public function save($dbConn = Null)
88
-    {
89
-        if (is_null($dbConn)) {
90
-            $dbConn = new DatabaseConnection();
91
-        }
92
-
93
-        $boolCommit = false;
94
-
95
-        if ($this->properties['id']) {
96
-
97
-            $allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn);
98
-
99
-            if ($this->checkIfRecordIsEmpty($allData)) {
100
-                $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn);
101
-
102
-                if ($boolCommit) {
103
-                    return true;
104
-                }
105
-
106
-                throw NoRecordUpdateException::create('Record not updated successfully');
107
-            }
108
-
109
-            throw EmptyArrayException::create("Value passed didn't match any record");
110
-        }
111
-
112
-        $boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn);
113
-
114
-        if ($boolCommit) {
115
-            return true;
116
-        }
117
-
118
-        throw NoRecordInsertionException::create('Record not created successfully');
119
-    }
120
-
121
-    /**
122
-     * This method find a record by id.
123
-     *
124
-     * @params int id
125
-     *
126
-     * @throws NoArgumentPassedToFunctionException
127
-     *
128
-     * @return object
129
-     */
130
-    public static function find($id)
131
-    {
132
-        $num_args = (int) func_num_args(); // get number of arguments passed to this function
133
-        if ($num_args == 0 || $num_args > 1) {
134
-            throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
135
-        }
136
-
137
-        if ($id == '') {
138
-            throw NullArgumentPassedToFunctionException::create('This function expect a value');
139
-        }
140
-
141
-        $staticFindInstance = new static();
142
-        $staticFindInstance->id = $id == '' ? false : $id;
143
-
144
-        return $staticFindInstance;
145
-    }
146
-
147
-    /**
148
-     * This method delete a row from the table by the row id.
149
-     *
150
-     * @params int id
151
-     *
152
-     * @throws NoRecordDeletionException;
153
-     *
154
-     * @return bool true or false
155
-     */
156
-    public static function destroy($id)
157
-    {
158
-        $boolDeleted = false;
159
-
160
-        $num_args = (int) func_num_args(); // get number of arguments passed to this function
161
-
162
-        if ($num_args == 0 || $num_args > 1) {
163
-            throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
164
-        }
165
-
166
-        $boolDeleted = DatabaseHandler::delete($id, self::getClassName());
167
-
168
-        if ($boolDeleted) {
169
-            return true;
170
-        }
171
-
172
-        throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record');
173
-    }
11
+	// Inject the inflector trait
12
+	use Inflector;
13
+
14
+	// Private variable that contains instance of database
15
+	protected $databaseModel;
16
+
17
+	// Class variable holding class name pluralized
18
+	protected $tableName;
19
+
20
+	// Properties will later contain key, value pairs from the magic setter, getter methods
21
+	protected $properties = [];
22
+
23
+	public function __construct()
24
+	{
25
+		$this->tableName = $this->getClassName();
26
+
27
+		$this->databaseModel = new DatabaseHandler($this->tableName);
28
+
29
+		$this->properties['id'] = 0;
30
+	}
31
+
32
+	/**
33
+	 * The magic getter method.
34
+	 *
35
+	 * @params key
36
+	 *
37
+	 * @return array key
38
+	 */
39
+	public function __get($key)
40
+	{
41
+		$this->properties[$key];
42
+	}
43
+
44
+	/**
45
+	 * The magic setter method.
46
+	 *
47
+	 * @params property, key
48
+	 *
49
+	 * @return array associative array properties
50
+	 */
51
+	public function __set($property, $value)
52
+	{
53
+		$this->properties[$property] = $value;
54
+	}
55
+
56
+	/**
57
+	 * This method gets all the record from a particular table.
58
+	 *
59
+	 * @params void
60
+	 *
61
+	 * @throws NoRecordFoundException
62
+	 *
63
+	 * @return associative array
64
+	 */
65
+	public static function getAll()
66
+	{
67
+		$allData = DatabaseHandler::read($id = false, self::getClassName());
68
+
69
+		if (count($allData) > 0) {
70
+			return $allData;
71
+		}
72
+
73
+		throw NoRecordFoundException::create('There is no record to display');
74
+	}
75
+
76
+	/**
77
+	 * This method create or update record in a database table.
78
+	 *
79
+	 * @params void
80
+	 *
81
+	 * @throws EmptyArrayException
82
+	 * @throws NoRecordInsertionException
83
+	 * @throws NoRecordUpdateException
84
+	 *
85
+	 * @return bool true or false;
86
+	 */
87
+	public function save($dbConn = Null)
88
+	{
89
+		if (is_null($dbConn)) {
90
+			$dbConn = new DatabaseConnection();
91
+		}
92
+
93
+		$boolCommit = false;
94
+
95
+		if ($this->properties['id']) {
96
+
97
+			$allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn);
98
+
99
+			if ($this->checkIfRecordIsEmpty($allData)) {
100
+				$boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn);
101
+
102
+				if ($boolCommit) {
103
+					return true;
104
+				}
105
+
106
+				throw NoRecordUpdateException::create('Record not updated successfully');
107
+			}
108
+
109
+			throw EmptyArrayException::create("Value passed didn't match any record");
110
+		}
111
+
112
+		$boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn);
113
+
114
+		if ($boolCommit) {
115
+			return true;
116
+		}
117
+
118
+		throw NoRecordInsertionException::create('Record not created successfully');
119
+	}
120
+
121
+	/**
122
+	 * This method find a record by id.
123
+	 *
124
+	 * @params int id
125
+	 *
126
+	 * @throws NoArgumentPassedToFunctionException
127
+	 *
128
+	 * @return object
129
+	 */
130
+	public static function find($id)
131
+	{
132
+		$num_args = (int) func_num_args(); // get number of arguments passed to this function
133
+		if ($num_args == 0 || $num_args > 1) {
134
+			throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
135
+		}
136
+
137
+		if ($id == '') {
138
+			throw NullArgumentPassedToFunctionException::create('This function expect a value');
139
+		}
140
+
141
+		$staticFindInstance = new static();
142
+		$staticFindInstance->id = $id == '' ? false : $id;
143
+
144
+		return $staticFindInstance;
145
+	}
146
+
147
+	/**
148
+	 * This method delete a row from the table by the row id.
149
+	 *
150
+	 * @params int id
151
+	 *
152
+	 * @throws NoRecordDeletionException;
153
+	 *
154
+	 * @return bool true or false
155
+	 */
156
+	public static function destroy($id)
157
+	{
158
+		$boolDeleted = false;
159
+
160
+		$num_args = (int) func_num_args(); // get number of arguments passed to this function
161
+
162
+		if ($num_args == 0 || $num_args > 1) {
163
+			throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
164
+		}
165
+
166
+		$boolDeleted = DatabaseHandler::delete($id, self::getClassName());
167
+
168
+		if ($boolDeleted) {
169
+			return true;
170
+		}
171
+
172
+		throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record');
173
+	}
174 174
 
175 175
    /**
176 176
     * This method return the current class name
@@ -180,26 +180,26 @@  discard block
 block discarded – undo
180 180
     */
181 181
    public static function getClassName()
182 182
    {
183
-       $tableName = preg_split('/(?=[A-Z])/', get_called_class());
183
+	   $tableName = preg_split('/(?=[A-Z])/', get_called_class());
184 184
 
185
-       $className = end($tableName);
185
+	   $className = end($tableName);
186 186
 
187
-       return self::pluralize(strtolower($className));
187
+	   return self::pluralize(strtolower($className));
188 188
    }
189 189
 
190
-    /**
191
-     * This method check if the argument passed to this function is an array.
192
-     *
193
-     * @param $arrayOfRecord
194
-     *
195
-     * @return bool
196
-     */
197
-    public function checkIfRecordIsEmpty($arrayOfRecord)
198
-    {
199
-        if (count($arrayOfRecord) > 0) {
200
-            return true;
201
-        }
202
-
203
-        return false;
204
-    }
190
+	/**
191
+	 * This method check if the argument passed to this function is an array.
192
+	 *
193
+	 * @param $arrayOfRecord
194
+	 *
195
+	 * @return bool
196
+	 */
197
+	public function checkIfRecordIsEmpty($arrayOfRecord)
198
+	{
199
+		if (count($arrayOfRecord) > 0) {
200
+			return true;
201
+		}
202
+
203
+		return false;
204
+	}
205 205
 }
Please login to merge, or discard this patch.