@@ -21,6 +21,7 @@ discard block |
||
21 | 21 | |
22 | 22 | /** |
23 | 23 | * This is a constructor; a default method that will be called automatically during class instantiation |
24 | + * @param string|false $modelClassName |
|
24 | 25 | */ |
25 | 26 | public function __construct($modelClassName, $dbConn = Null) |
26 | 27 | { |
@@ -35,6 +36,7 @@ discard block |
||
35 | 36 | /** |
36 | 37 | * This method create a record and store it in a table row |
37 | 38 | * @params associative array, string tablename |
39 | + * @param string|false $tableName |
|
38 | 40 | * @return boolean true or false |
39 | 41 | */ |
40 | 42 | public function create($associative1DArray, $tableName, $dbConn = Null) |
@@ -69,6 +71,7 @@ discard block |
||
69 | 71 | /** |
70 | 72 | * This method updates any table by supplying 3 parameter |
71 | 73 | * @params: $updateParams, $tableName, $associative1DArray |
74 | + * @param string|false $tableName |
|
72 | 75 | * @return boolean true or false |
73 | 76 | */ |
74 | 77 | public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
@@ -101,6 +104,7 @@ discard block |
||
101 | 104 | /** |
102 | 105 | * This method retrieves record from a table |
103 | 106 | * @params int id, string tableName |
107 | + * @param string|false $tableName |
|
104 | 108 | * @return array |
105 | 109 | */ |
106 | 110 | public static function read($id, $tableName, $dbConn = Null) |
@@ -131,6 +135,7 @@ discard block |
||
131 | 135 | /** |
132 | 136 | * This method deletes a record from a table row |
133 | 137 | * @params int id, string tableName |
138 | + * @param string|false $tableName |
|
134 | 139 | * @return boolean true or false |
135 | 140 | */ |
136 | 141 | public static function delete($id, $tableName, $dbConn = Null) |
@@ -163,7 +168,7 @@ discard block |
||
163 | 168 | |
164 | 169 | /** |
165 | 170 | * This method returns sql query |
166 | - * @param $sql |
|
171 | + * @param string $sql |
|
167 | 172 | * @return string |
168 | 173 | */ |
169 | 174 | public function prepareUpdateQuery($sql) |
@@ -204,6 +209,7 @@ discard block |
||
204 | 209 | * This method returns column fields of a particular table |
205 | 210 | * @param $table |
206 | 211 | * @param $conn |
212 | + * @param DatabaseConnection $dbConn |
|
207 | 213 | * @return array |
208 | 214 | */ |
209 | 215 | public function getColumnNames($table, $dbConn = Null) { |
@@ -14,89 +14,89 @@ discard block |
||
14 | 14 | use Laztopaz\potatoORM\EmptyArrayException; |
15 | 15 | |
16 | 16 | class DatabaseHandler { |
17 | - private $tableFields; |
|
18 | - private $dbHelperInstance; |
|
19 | - private $dbConnection; |
|
20 | - private $model; |
|
17 | + private $tableFields; |
|
18 | + private $dbHelperInstance; |
|
19 | + private $dbConnection; |
|
20 | + private $model; |
|
21 | 21 | |
22 | - /** |
|
23 | - * This is a constructor; a default method that will be called automatically during class instantiation |
|
24 | - */ |
|
25 | - public function __construct($modelClassName, $dbConn = Null) |
|
26 | - { |
|
27 | - if (is_null($dbConn)) { |
|
28 | - $this->dbConnection = new DatabaseConnection(); |
|
29 | - } else { |
|
30 | - $this->dbConnection = $dbConn; |
|
31 | - } |
|
32 | - $this->model = $modelClassName; |
|
33 | - } |
|
22 | + /** |
|
23 | + * This is a constructor; a default method that will be called automatically during class instantiation |
|
24 | + */ |
|
25 | + public function __construct($modelClassName, $dbConn = Null) |
|
26 | + { |
|
27 | + if (is_null($dbConn)) { |
|
28 | + $this->dbConnection = new DatabaseConnection(); |
|
29 | + } else { |
|
30 | + $this->dbConnection = $dbConn; |
|
31 | + } |
|
32 | + $this->model = $modelClassName; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * This method create a record and store it in a table row |
|
37 | - * @params associative array, string tablename |
|
38 | - * @return boolean true or false |
|
39 | - */ |
|
40 | - public function create($associative1DArray, $tableName, $dbConn = Null) |
|
41 | - { |
|
42 | - $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
43 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields,$associative1DArray); |
|
44 | - if (count($unexpectedFields) > 0) { |
|
45 | - throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields,"needs to be created as table field"); |
|
46 | - } |
|
47 | - unset($associative1DArray[0]); |
|
48 | - if (is_null($dbConn)) { |
|
49 | - $dbConn = $this->dbConnection; |
|
50 | - } |
|
51 | - return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
52 | - } |
|
35 | + /** |
|
36 | + * This method create a record and store it in a table row |
|
37 | + * @params associative array, string tablename |
|
38 | + * @return boolean true or false |
|
39 | + */ |
|
40 | + public function create($associative1DArray, $tableName, $dbConn = Null) |
|
41 | + { |
|
42 | + $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
43 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields,$associative1DArray); |
|
44 | + if (count($unexpectedFields) > 0) { |
|
45 | + throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields,"needs to be created as table field"); |
|
46 | + } |
|
47 | + unset($associative1DArray[0]); |
|
48 | + if (is_null($dbConn)) { |
|
49 | + $dbConn = $this->dbConnection; |
|
50 | + } |
|
51 | + return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
52 | + } |
|
53 | 53 | |
54 | - private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
55 | - { |
|
56 | - $insertQuery = 'INSERT INTO '.$tableName; |
|
57 | - $TableValues = implode(',',array_keys($associative1DArray)); |
|
58 | - foreach ($associative1DArray as $field => $value) { |
|
59 | - $FormValues[] = "'".trim(addslashes($value))."'"; |
|
60 | - } |
|
61 | - $splittedTableValues = implode(',', $FormValues); |
|
62 | - $insertQuery.= ' ('.$TableValues.')'; |
|
63 | - $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
64 | - $executeQuery = $dbConn->exec($insertQuery); |
|
65 | - return $executeQuery ? : false; |
|
54 | + private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
55 | + { |
|
56 | + $insertQuery = 'INSERT INTO '.$tableName; |
|
57 | + $TableValues = implode(',',array_keys($associative1DArray)); |
|
58 | + foreach ($associative1DArray as $field => $value) { |
|
59 | + $FormValues[] = "'".trim(addslashes($value))."'"; |
|
60 | + } |
|
61 | + $splittedTableValues = implode(',', $FormValues); |
|
62 | + $insertQuery.= ' ('.$TableValues.')'; |
|
63 | + $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
64 | + $executeQuery = $dbConn->exec($insertQuery); |
|
65 | + return $executeQuery ? : false; |
|
66 | 66 | |
67 | 67 | } |
68 | 68 | |
69 | - /** |
|
70 | - * This method updates any table by supplying 3 parameter |
|
71 | - * @params: $updateParams, $tableName, $associative1DArray |
|
72 | - * @return boolean true or false |
|
73 | - */ |
|
74 | - public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
|
75 | - { |
|
76 | - $sql = ""; |
|
77 | - if (is_null($dbConn)) { |
|
78 | - $dbConn = $this->dbConnection; |
|
79 | - } |
|
80 | - $updateSql = "UPDATE `$tableName` SET "; |
|
81 | - unset($associative1DArray['id']); |
|
82 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
|
69 | + /** |
|
70 | + * This method updates any table by supplying 3 parameter |
|
71 | + * @params: $updateParams, $tableName, $associative1DArray |
|
72 | + * @return boolean true or false |
|
73 | + */ |
|
74 | + public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
|
75 | + { |
|
76 | + $sql = ""; |
|
77 | + if (is_null($dbConn)) { |
|
78 | + $dbConn = $this->dbConnection; |
|
79 | + } |
|
80 | + $updateSql = "UPDATE `$tableName` SET "; |
|
81 | + unset($associative1DArray['id']); |
|
82 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
|
83 | 83 | |
84 | - if (count($unexpectedFields) > 0) { |
|
85 | - throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields, "needs to be created as table field"); |
|
86 | - } |
|
87 | - foreach($associative1DArray as $field => $value) { |
|
88 | - $sql .= "`$field` = '$value'".","; |
|
89 | - } |
|
84 | + if (count($unexpectedFields) > 0) { |
|
85 | + throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields, "needs to be created as table field"); |
|
86 | + } |
|
87 | + foreach($associative1DArray as $field => $value) { |
|
88 | + $sql .= "`$field` = '$value'".","; |
|
89 | + } |
|
90 | 90 | |
91 | - $updateSql .= $this->prepareUpdateQuery($sql); |
|
91 | + $updateSql .= $this->prepareUpdateQuery($sql); |
|
92 | 92 | |
93 | - foreach ($updateParams as $key => $val) { |
|
94 | - $updateSql .= " WHERE $key = $val"; |
|
95 | - } |
|
96 | - $stmt = $dbConn->prepare($updateSql); |
|
97 | - $boolResponse = $stmt->execute(); |
|
98 | - return $boolResponse ? : false; |
|
99 | - } |
|
93 | + foreach ($updateParams as $key => $val) { |
|
94 | + $updateSql .= " WHERE $key = $val"; |
|
95 | + } |
|
96 | + $stmt = $dbConn->prepare($updateSql); |
|
97 | + $boolResponse = $stmt->execute(); |
|
98 | + return $boolResponse ? : false; |
|
99 | + } |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * This method retrieves record from a table |
@@ -108,22 +108,22 @@ discard block |
||
108 | 108 | $tableData = []; |
109 | 109 | |
110 | 110 | if (is_null($dbConn)) { |
111 | - $dbConn = new DatabaseConnection(); |
|
111 | + $dbConn = new DatabaseConnection(); |
|
112 | 112 | } |
113 | 113 | $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
114 | 114 | |
115 | 115 | try { |
116 | - $stmt = $dbConn->prepare($sql); |
|
117 | - $stmt->bindValue(':table', $tableName); |
|
118 | - $stmt->bindValue(':id', $id); |
|
119 | - $stmt->execute(); |
|
116 | + $stmt = $dbConn->prepare($sql); |
|
117 | + $stmt->bindValue(':table', $tableName); |
|
118 | + $stmt->bindValue(':id', $id); |
|
119 | + $stmt->execute(); |
|
120 | 120 | } catch (PDOException $e) { |
121 | - return $e->getMessage(); |
|
121 | + return $e->getMessage(); |
|
122 | 122 | } |
123 | 123 | $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
124 | 124 | |
125 | 125 | foreach($results as $result) { |
126 | - array_push($tableData, $result); |
|
126 | + array_push($tableData, $result); |
|
127 | 127 | } |
128 | 128 | return $tableData; |
129 | 129 | } |
@@ -135,13 +135,13 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public static function delete($id, $tableName, $dbConn = Null) |
137 | 137 | { |
138 | - if (is_null($dbConn)) { |
|
139 | - $dbConn = new DatabaseConnection(); |
|
140 | - } |
|
138 | + if (is_null($dbConn)) { |
|
139 | + $dbConn = new DatabaseConnection(); |
|
140 | + } |
|
141 | 141 | |
142 | - $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
143 | - $boolResponse = $dbConn->exec($sql); |
|
144 | - return $boolResponse ? : false; |
|
142 | + $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
143 | + $boolResponse = $dbConn->exec($sql); |
|
144 | + return $boolResponse ? : false; |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -152,13 +152,13 @@ discard block |
||
152 | 152 | */ |
153 | 153 | public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray) |
154 | 154 | { |
155 | - $unexpectedFields = []; |
|
156 | - foreach ($userSetterArray as $key => $val) { |
|
157 | - if (!in_array($key,$tableColumn)) { |
|
158 | - $unexpectedFields[] = $key; |
|
159 | - } |
|
160 | - } |
|
161 | - return $unexpectedFields; |
|
155 | + $unexpectedFields = []; |
|
156 | + foreach ($userSetterArray as $key => $val) { |
|
157 | + if (!in_array($key,$tableColumn)) { |
|
158 | + $unexpectedFields[] = $key; |
|
159 | + } |
|
160 | + } |
|
161 | + return $unexpectedFields; |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | /** |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | */ |
169 | 169 | public function prepareUpdateQuery($sql) |
170 | 170 | { |
171 | - $splittedQuery = explode(",",$sql); |
|
172 | - array_pop($splittedQuery); |
|
173 | - $mergeData = implode(",",$splittedQuery); |
|
174 | - return $mergeData; |
|
171 | + $splittedQuery = explode(",",$sql); |
|
172 | + array_pop($splittedQuery); |
|
173 | + $mergeData = implode(",",$splittedQuery); |
|
174 | + return $mergeData; |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | /** |
@@ -183,21 +183,21 @@ discard block |
||
183 | 183 | */ |
184 | 184 | public function findAndWhere($params, $tableName, $dbConn = Null) |
185 | 185 | { |
186 | - if (is_null($dbConn)) { |
|
187 | - $dbConn = $this->dbConnection; |
|
188 | - } |
|
189 | - if (is_array($params) && !empty($params)) { |
|
190 | - $sql = "SELECT * FROM ".$tableName; |
|
191 | - foreach ($params as $key => $val) { |
|
192 | - $sql .= " WHERE `$key` = '$val'"; |
|
193 | - } |
|
194 | - $statement = $dbConn->prepare($sql); |
|
195 | - $statement->execute(); |
|
196 | - $returnedRowNumbers = $statement->rowCount(); |
|
186 | + if (is_null($dbConn)) { |
|
187 | + $dbConn = $this->dbConnection; |
|
188 | + } |
|
189 | + if (is_array($params) && !empty($params)) { |
|
190 | + $sql = "SELECT * FROM ".$tableName; |
|
191 | + foreach ($params as $key => $val) { |
|
192 | + $sql .= " WHERE `$key` = '$val'"; |
|
193 | + } |
|
194 | + $statement = $dbConn->prepare($sql); |
|
195 | + $statement->execute(); |
|
196 | + $returnedRowNumbers = $statement->rowCount(); |
|
197 | 197 | |
198 | - return $returnedRowNumbers ? true : false; |
|
199 | - } |
|
200 | - throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
198 | + return $returnedRowNumbers ? true : false; |
|
199 | + } |
|
200 | + throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |
@@ -207,22 +207,22 @@ discard block |
||
207 | 207 | * @return array |
208 | 208 | */ |
209 | 209 | public function getColumnNames($table, $dbConn = Null) { |
210 | - $tableFields = []; |
|
210 | + $tableFields = []; |
|
211 | 211 | |
212 | - if (is_null($dbConn)) { |
|
213 | - $dbConn = $this->dbConnection; |
|
214 | - } |
|
215 | - $sql = "SHOW COLUMNS FROM ".$table; |
|
212 | + if (is_null($dbConn)) { |
|
213 | + $dbConn = $this->dbConnection; |
|
214 | + } |
|
215 | + $sql = "SHOW COLUMNS FROM ".$table; |
|
216 | 216 | |
217 | - $stmt = $dbConn->prepare($sql); |
|
218 | - $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
219 | - $stmt->execute(); |
|
220 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
217 | + $stmt = $dbConn->prepare($sql); |
|
218 | + $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
219 | + $stmt->execute(); |
|
220 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
221 | 221 | |
222 | - foreach($results as $result) { |
|
223 | - array_push($tableFields, $result['Field']); |
|
224 | - } |
|
225 | - return $tableFields; |
|
222 | + foreach($results as $result) { |
|
223 | + array_push($tableFields, $result['Field']); |
|
224 | + } |
|
225 | + return $tableFields; |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | } |
@@ -20,124 +20,124 @@ discard block |
||
20 | 20 | |
21 | 21 | class BaseClass implements InterfaceBaseClass |
22 | 22 | { |
23 | - protected $databaseModel; // Private variable that contains instance of database |
|
24 | - protected $tableName; // Class variable holding class name pluralized |
|
25 | - protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
26 | - use Inflector; // Inject the inflector trait |
|
23 | + protected $databaseModel; // Private variable that contains instance of database |
|
24 | + protected $tableName; // Class variable holding class name pluralized |
|
25 | + protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
26 | + use Inflector; // Inject the inflector trait |
|
27 | 27 | |
28 | - public function __construct() |
|
29 | - { |
|
30 | - $this->tableName = $this->getClassName(); |
|
31 | - $this->databaseModel = new DatabaseHandler($this->tableName); |
|
32 | - $this->properties['id'] = 0; |
|
33 | - } |
|
28 | + public function __construct() |
|
29 | + { |
|
30 | + $this->tableName = $this->getClassName(); |
|
31 | + $this->databaseModel = new DatabaseHandler($this->tableName); |
|
32 | + $this->properties['id'] = 0; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * The magic getter method |
|
37 | - * @params key |
|
38 | - * @return array key |
|
39 | - */ |
|
40 | - public function __get($key) |
|
41 | - { |
|
42 | - $this->properties[$key]; |
|
43 | - } |
|
35 | + /** |
|
36 | + * The magic getter method |
|
37 | + * @params key |
|
38 | + * @return array key |
|
39 | + */ |
|
40 | + public function __get($key) |
|
41 | + { |
|
42 | + $this->properties[$key]; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * The magic setter method |
|
47 | - * @params property, key |
|
48 | - * @return array associative array properties |
|
49 | - */ |
|
50 | - public function __set($property, $value) |
|
51 | - { |
|
52 | - $this->properties[$property] = $value; |
|
53 | - } |
|
45 | + /** |
|
46 | + * The magic setter method |
|
47 | + * @params property, key |
|
48 | + * @return array associative array properties |
|
49 | + */ |
|
50 | + public function __set($property, $value) |
|
51 | + { |
|
52 | + $this->properties[$property] = $value; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * This method gets all the record from a particular table |
|
57 | - * @params void |
|
58 | - * @return associative array |
|
59 | - * @throws NoRecordFoundException |
|
60 | - */ |
|
61 | - public static function getAll() |
|
62 | - { |
|
63 | - $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
64 | - if (count($allData) > 0) { |
|
65 | - return $allData; |
|
66 | - } |
|
67 | - throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
68 | - } |
|
55 | + /** |
|
56 | + * This method gets all the record from a particular table |
|
57 | + * @params void |
|
58 | + * @return associative array |
|
59 | + * @throws NoRecordFoundException |
|
60 | + */ |
|
61 | + public static function getAll() |
|
62 | + { |
|
63 | + $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
64 | + if (count($allData) > 0) { |
|
65 | + return $allData; |
|
66 | + } |
|
67 | + throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * This method create or update record in a database table |
|
72 | - * @params void |
|
73 | - * @return bool true or false; |
|
74 | - * @throws EmptyArrayException |
|
75 | - * @throws NoRecordInsertionException |
|
76 | - * @throws NoRecordUpdateException |
|
77 | - */ |
|
78 | - public function save() |
|
79 | - { |
|
80 | - $boolCommit = false; |
|
81 | - if ($this->properties['id']) { |
|
82 | - $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
83 | - if ($this->checkIfRecordIsEmpty($allData)) { |
|
84 | - $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
85 | - if ($boolCommit) { |
|
86 | - return true; |
|
87 | - } |
|
88 | - throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
89 | - } |
|
90 | - throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
91 | - } |
|
92 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
93 | - if ($boolCommit) { |
|
94 | - return true; |
|
95 | - } |
|
96 | - throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
97 | - } |
|
70 | + /** |
|
71 | + * This method create or update record in a database table |
|
72 | + * @params void |
|
73 | + * @return bool true or false; |
|
74 | + * @throws EmptyArrayException |
|
75 | + * @throws NoRecordInsertionException |
|
76 | + * @throws NoRecordUpdateException |
|
77 | + */ |
|
78 | + public function save() |
|
79 | + { |
|
80 | + $boolCommit = false; |
|
81 | + if ($this->properties['id']) { |
|
82 | + $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
83 | + if ($this->checkIfRecordIsEmpty($allData)) { |
|
84 | + $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
85 | + if ($boolCommit) { |
|
86 | + return true; |
|
87 | + } |
|
88 | + throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
89 | + } |
|
90 | + throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
91 | + } |
|
92 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
93 | + if ($boolCommit) { |
|
94 | + return true; |
|
95 | + } |
|
96 | + throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * This method find a record by id |
|
101 | - * @params int id |
|
102 | - * @return Object |
|
103 | - * @throws NoArgumentPassedToFunctionException |
|
104 | - */ |
|
105 | - public static function find($id) |
|
106 | - { |
|
107 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
108 | - if ($num_args == 0 || $num_args > 1) { |
|
109 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
110 | - } |
|
111 | - if ($id == "") { |
|
112 | - throw NullArgumentPassedToFunction::ckeckNullArgumentPassedToFunction("This function expect a value"); |
|
113 | - } |
|
114 | - $staticFindInstance = new static(); |
|
115 | - $staticFindInstance->id = $id == "" ? false : $id; |
|
116 | - return $staticFindInstance; |
|
99 | + /** |
|
100 | + * This method find a record by id |
|
101 | + * @params int id |
|
102 | + * @return Object |
|
103 | + * @throws NoArgumentPassedToFunctionException |
|
104 | + */ |
|
105 | + public static function find($id) |
|
106 | + { |
|
107 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
108 | + if ($num_args == 0 || $num_args > 1) { |
|
109 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
110 | + } |
|
111 | + if ($id == "") { |
|
112 | + throw NullArgumentPassedToFunction::ckeckNullArgumentPassedToFunction("This function expect a value"); |
|
113 | + } |
|
114 | + $staticFindInstance = new static(); |
|
115 | + $staticFindInstance->id = $id == "" ? false : $id; |
|
116 | + return $staticFindInstance; |
|
117 | 117 | |
118 | - } |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * This method delete a row from the table by the row id |
|
122 | - * @params int id |
|
123 | - * @return boolean true or false |
|
124 | - * @throws NoRecordDeletionException; |
|
125 | - */ |
|
126 | - public static function destroy($id) |
|
127 | - { |
|
128 | - $boolDeleted = false; |
|
129 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
120 | + /** |
|
121 | + * This method delete a row from the table by the row id |
|
122 | + * @params int id |
|
123 | + * @return boolean true or false |
|
124 | + * @throws NoRecordDeletionException; |
|
125 | + */ |
|
126 | + public static function destroy($id) |
|
127 | + { |
|
128 | + $boolDeleted = false; |
|
129 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
130 | 130 | |
131 | - if ($num_args == 0 || $num_args > 1) { |
|
132 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
133 | - } |
|
134 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
135 | - if ($boolDeleted) { |
|
136 | - return true; |
|
137 | - } |
|
138 | - throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
131 | + if ($num_args == 0 || $num_args > 1) { |
|
132 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
133 | + } |
|
134 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
135 | + if ($boolDeleted) { |
|
136 | + return true; |
|
137 | + } |
|
138 | + throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | 142 | /** |
143 | 143 | * This method return the current class name |
@@ -156,12 +156,12 @@ discard block |
||
156 | 156 | * @param $arrayOfRecord |
157 | 157 | * @return bool |
158 | 158 | */ |
159 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
160 | - { |
|
161 | - if (count($arrayOfRecord) > 0) { |
|
162 | - return true; |
|
163 | - } |
|
164 | - return false; |
|
165 | - } |
|
159 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
160 | + { |
|
161 | + if (count($arrayOfRecord) > 0) { |
|
162 | + return true; |
|
163 | + } |
|
164 | + return false; |
|
165 | + } |
|
166 | 166 | |
167 | 167 | } |
@@ -14,73 +14,73 @@ |
||
14 | 14 | |
15 | 15 | class DatabaseConnection extends \PDO { |
16 | 16 | |
17 | - private $databaseName; |
|
18 | - private $databaseHost; |
|
19 | - private $databaseDriver; |
|
20 | - private $databaseUsername; |
|
21 | - private $databasePassword; |
|
17 | + private $databaseName; |
|
18 | + private $databaseHost; |
|
19 | + private $databaseDriver; |
|
20 | + private $databaseUsername; |
|
21 | + private $databasePassword; |
|
22 | 22 | |
23 | - public function __construct() |
|
24 | - { |
|
25 | - $this->loadEnv(); // load the environment variables |
|
26 | - $this->databaseName = getenv('databaseName'); |
|
27 | - $this->databaseHost = getenv('databaseHost'); |
|
28 | - $this->databaseDriver = getenv('databaseDriver'); |
|
29 | - $this->databaseUsername = getenv('databaseUsername'); |
|
30 | - $this->databasePassword = getenv('databasePassword'); |
|
23 | + public function __construct() |
|
24 | + { |
|
25 | + $this->loadEnv(); // load the environment variables |
|
26 | + $this->databaseName = getenv('databaseName'); |
|
27 | + $this->databaseHost = getenv('databaseHost'); |
|
28 | + $this->databaseDriver = getenv('databaseDriver'); |
|
29 | + $this->databaseUsername = getenv('databaseUsername'); |
|
30 | + $this->databasePassword = getenv('databasePassword'); |
|
31 | 31 | |
32 | - try { |
|
33 | - $options = [ |
|
34 | - PDO::ATTR_PERSISTENT => true, |
|
35 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
|
36 | - ]; |
|
37 | - parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
32 | + try { |
|
33 | + $options = [ |
|
34 | + PDO::ATTR_PERSISTENT => true, |
|
35 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
|
36 | + ]; |
|
37 | + parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
38 | 38 | |
39 | - } catch(PDOException $e) { |
|
40 | - return $e->getMessage(); |
|
41 | - } |
|
39 | + } catch(PDOException $e) { |
|
40 | + return $e->getMessage(); |
|
41 | + } |
|
42 | 42 | |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * This method determines the driver to be used for appropriate database server |
|
47 | - * @params void |
|
48 | - * @return string dsn |
|
49 | - */ |
|
50 | - public function getDatabaseDriver() |
|
51 | - { |
|
52 | - $dsn = ""; |
|
45 | + /** |
|
46 | + * This method determines the driver to be used for appropriate database server |
|
47 | + * @params void |
|
48 | + * @return string dsn |
|
49 | + */ |
|
50 | + public function getDatabaseDriver() |
|
51 | + { |
|
52 | + $dsn = ""; |
|
53 | 53 | |
54 | - switch ($this->databaseDriver) |
|
55 | - { |
|
56 | - case 'mysql': |
|
57 | - // Set DSN |
|
58 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
59 | - break; |
|
60 | - case 'sqlite': |
|
61 | - // Set DSN |
|
62 | - $dsn = 'sqlite:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
63 | - break; |
|
64 | - case 'pgsql': |
|
65 | - // Set DSN |
|
66 | - $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
67 | - break; |
|
68 | - default: |
|
69 | - // Set DSN |
|
70 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
71 | - } |
|
72 | - return $dsn; |
|
73 | - } |
|
54 | + switch ($this->databaseDriver) |
|
55 | + { |
|
56 | + case 'mysql': |
|
57 | + // Set DSN |
|
58 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
59 | + break; |
|
60 | + case 'sqlite': |
|
61 | + // Set DSN |
|
62 | + $dsn = 'sqlite:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
63 | + break; |
|
64 | + case 'pgsql': |
|
65 | + // Set DSN |
|
66 | + $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
67 | + break; |
|
68 | + default: |
|
69 | + // Set DSN |
|
70 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName; |
|
71 | + } |
|
72 | + return $dsn; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Load Dotenv to grant getenv() access to environment variables in .env file |
|
77 | - */ |
|
78 | - public function loadEnv() |
|
79 | - { |
|
80 | - if (!getenv("APP_ENV")) { |
|
81 | - $dotenv = new Dotenv(__DIR__.'/../../'); |
|
82 | - $dotenv->load(); |
|
83 | - } |
|
84 | - } |
|
75 | + /** |
|
76 | + * Load Dotenv to grant getenv() access to environment variables in .env file |
|
77 | + */ |
|
78 | + public function loadEnv() |
|
79 | + { |
|
80 | + if (!getenv("APP_ENV")) { |
|
81 | + $dotenv = new Dotenv(__DIR__.'/../../'); |
|
82 | + $dotenv->load(); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | 86 | } |