@@ -10,5 +10,5 @@ |
||
10 | 10 | |
11 | 11 | class Inflector { |
12 | 12 | |
13 | - use Inflector; |
|
13 | + use Inflector; |
|
14 | 14 | } |
@@ -16,6 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | /** |
18 | 18 | * This is a constructor; a default method that will be called automatically during class instantiation. |
19 | + * @param string|false $modelClassName |
|
19 | 20 | */ |
20 | 21 | public function __construct($modelClassName, $dbConn = null) |
21 | 22 | { |
@@ -32,6 +33,7 @@ discard block |
||
32 | 33 | * This method create a record and store it in a table row. |
33 | 34 | * |
34 | 35 | * @params associative array, string tablename |
36 | + * @param string|false $tableName |
|
35 | 37 | * |
36 | 38 | * @return bool true or false |
37 | 39 | */ |
@@ -91,6 +93,7 @@ discard block |
||
91 | 93 | * This method updates any table by supplying 3 parameter. |
92 | 94 | * |
93 | 95 | * @params: $updateParams, $tableName, $associative1DArray |
96 | + * @param string|false $tableName |
|
94 | 97 | * |
95 | 98 | * @return bool true or false |
96 | 99 | */ |
@@ -137,6 +140,7 @@ discard block |
||
137 | 140 | * This method retrieves record from a table. |
138 | 141 | * |
139 | 142 | * @params int id, string tableName |
143 | + * @param string|false $tableName |
|
140 | 144 | * |
141 | 145 | * @return array |
142 | 146 | */ |
@@ -168,6 +172,7 @@ discard block |
||
168 | 172 | * This method deletes a record from a table row. |
169 | 173 | * |
170 | 174 | * @params int id, string tableName |
175 | + * @param string|false $tableName |
|
171 | 176 | * |
172 | 177 | * @return bool true or false |
173 | 178 | */ |
@@ -212,7 +217,7 @@ discard block |
||
212 | 217 | /** |
213 | 218 | * This method returns sql query. |
214 | 219 | * |
215 | - * @param $sql |
|
220 | + * @param string $sql |
|
216 | 221 | * |
217 | 222 | * @return string |
218 | 223 | */ |
@@ -265,6 +270,7 @@ discard block |
||
265 | 270 | * |
266 | 271 | * @param $table |
267 | 272 | * @param $conn |
273 | + * @param DatabaseConnection $dbConn |
|
268 | 274 | * |
269 | 275 | * @return array |
270 | 276 | */ |
@@ -9,129 +9,129 @@ discard block |
||
9 | 9 | use PDO; |
10 | 10 | class DatabaseHandler |
11 | 11 | { |
12 | - private $tableFields; |
|
13 | - private $dbHelperInstance; |
|
14 | - private $dbConnection; |
|
15 | - private $model; |
|
16 | - |
|
17 | - /** |
|
18 | - * This is a constructor; a default method that will be called automatically during class instantiation. |
|
19 | - */ |
|
20 | - public function __construct($modelClassName, $dbConn = null) |
|
21 | - { |
|
22 | - if (is_null($dbConn)) { |
|
23 | - $this->dbConnection = new DatabaseConnection(); |
|
24 | - } else { |
|
25 | - $this->dbConnection = $dbConn; |
|
26 | - } |
|
27 | - |
|
28 | - $this->model = $modelClassName; |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * This method create a record and store it in a table row. |
|
33 | - * |
|
34 | - * @params associative array, string tablename |
|
35 | - * |
|
36 | - * @return bool true or false |
|
37 | - */ |
|
38 | - public function create($associative1DArray, $tableName, $dbConn = null) |
|
39 | - { |
|
40 | - $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
41 | - |
|
42 | - $unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray); |
|
43 | - |
|
44 | - if (count($unexpectedFields) > 0) { |
|
45 | - throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
46 | - } |
|
47 | - |
|
48 | - unset($associative1DArray[0]); |
|
49 | - |
|
50 | - if (is_null($dbConn)) { |
|
51 | - $dbConn = $this->dbConnection; |
|
52 | - } |
|
53 | - |
|
54 | - return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * This method runs the insertion query. |
|
59 | - * |
|
60 | - * @param $dbConn |
|
61 | - * @param $tableName |
|
62 | - * @param $associative1DArray |
|
63 | - * |
|
64 | - * @return bool true |
|
65 | - */ |
|
66 | - private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
67 | - { |
|
68 | - $insertQuery = 'INSERT INTO '.$tableName; |
|
69 | - |
|
70 | - $TableValues = implode(',', array_keys($associative1DArray)); |
|
71 | - |
|
72 | - foreach ($associative1DArray as $field => $value) { |
|
73 | - $FormValues[] = "'".trim(addslashes($value))."'"; |
|
74 | - } |
|
75 | - |
|
76 | - $splittedTableValues = implode(',', $FormValues); |
|
77 | - |
|
78 | - $insertQuery .= ' ('.$TableValues.')'; |
|
79 | - $insertQuery .= ' VALUES ('.$splittedTableValues.')'; |
|
80 | - |
|
81 | - $executeQuery = $dbConn->exec($insertQuery); |
|
82 | - |
|
83 | - if ($executeQuery) { |
|
84 | - return true; |
|
85 | - } |
|
86 | - |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * This method updates any table by supplying 3 parameter. |
|
92 | - * |
|
93 | - * @params: $updateParams, $tableName, $associative1DArray |
|
94 | - * |
|
95 | - * @return bool true or false |
|
96 | - */ |
|
97 | - public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
98 | - { |
|
99 | - $sql = ''; |
|
100 | - |
|
101 | - if (is_null($dbConn)) { |
|
102 | - $dbConn = $this->dbConnection; |
|
103 | - } |
|
104 | - |
|
105 | - $updateSql = "UPDATE `$tableName` SET "; |
|
106 | - |
|
107 | - unset($associative1DArray['id']); |
|
108 | - |
|
109 | - $unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray); |
|
110 | - |
|
111 | - if (count($unexpectedFields) > 0) { |
|
112 | - throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
113 | - } |
|
12 | + private $tableFields; |
|
13 | + private $dbHelperInstance; |
|
14 | + private $dbConnection; |
|
15 | + private $model; |
|
16 | + |
|
17 | + /** |
|
18 | + * This is a constructor; a default method that will be called automatically during class instantiation. |
|
19 | + */ |
|
20 | + public function __construct($modelClassName, $dbConn = null) |
|
21 | + { |
|
22 | + if (is_null($dbConn)) { |
|
23 | + $this->dbConnection = new DatabaseConnection(); |
|
24 | + } else { |
|
25 | + $this->dbConnection = $dbConn; |
|
26 | + } |
|
27 | + |
|
28 | + $this->model = $modelClassName; |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * This method create a record and store it in a table row. |
|
33 | + * |
|
34 | + * @params associative array, string tablename |
|
35 | + * |
|
36 | + * @return bool true or false |
|
37 | + */ |
|
38 | + public function create($associative1DArray, $tableName, $dbConn = null) |
|
39 | + { |
|
40 | + $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
41 | + |
|
42 | + $unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray); |
|
43 | + |
|
44 | + if (count($unexpectedFields) > 0) { |
|
45 | + throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
46 | + } |
|
47 | + |
|
48 | + unset($associative1DArray[0]); |
|
49 | + |
|
50 | + if (is_null($dbConn)) { |
|
51 | + $dbConn = $this->dbConnection; |
|
52 | + } |
|
53 | + |
|
54 | + return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * This method runs the insertion query. |
|
59 | + * |
|
60 | + * @param $dbConn |
|
61 | + * @param $tableName |
|
62 | + * @param $associative1DArray |
|
63 | + * |
|
64 | + * @return bool true |
|
65 | + */ |
|
66 | + private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
67 | + { |
|
68 | + $insertQuery = 'INSERT INTO '.$tableName; |
|
69 | + |
|
70 | + $TableValues = implode(',', array_keys($associative1DArray)); |
|
71 | + |
|
72 | + foreach ($associative1DArray as $field => $value) { |
|
73 | + $FormValues[] = "'".trim(addslashes($value))."'"; |
|
74 | + } |
|
75 | + |
|
76 | + $splittedTableValues = implode(',', $FormValues); |
|
77 | + |
|
78 | + $insertQuery .= ' ('.$TableValues.')'; |
|
79 | + $insertQuery .= ' VALUES ('.$splittedTableValues.')'; |
|
80 | + |
|
81 | + $executeQuery = $dbConn->exec($insertQuery); |
|
82 | + |
|
83 | + if ($executeQuery) { |
|
84 | + return true; |
|
85 | + } |
|
86 | + |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * This method updates any table by supplying 3 parameter. |
|
92 | + * |
|
93 | + * @params: $updateParams, $tableName, $associative1DArray |
|
94 | + * |
|
95 | + * @return bool true or false |
|
96 | + */ |
|
97 | + public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
98 | + { |
|
99 | + $sql = ''; |
|
100 | + |
|
101 | + if (is_null($dbConn)) { |
|
102 | + $dbConn = $this->dbConnection; |
|
103 | + } |
|
104 | + |
|
105 | + $updateSql = "UPDATE `$tableName` SET "; |
|
106 | + |
|
107 | + unset($associative1DArray['id']); |
|
108 | + |
|
109 | + $unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray); |
|
110 | + |
|
111 | + if (count($unexpectedFields) > 0) { |
|
112 | + throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
113 | + } |
|
114 | 114 | |
115 | - foreach ($associative1DArray as $field => $value) { |
|
116 | - $sql .= "`$field` = '$value'".','; |
|
117 | - } |
|
115 | + foreach ($associative1DArray as $field => $value) { |
|
116 | + $sql .= "`$field` = '$value'".','; |
|
117 | + } |
|
118 | 118 | |
119 | - $updateSql .= $this->prepareUpdateQuery($sql); |
|
119 | + $updateSql .= $this->prepareUpdateQuery($sql); |
|
120 | 120 | |
121 | - foreach ($updateParams as $key => $val) { |
|
122 | - $updateSql .= " WHERE $key = $val"; |
|
123 | - } |
|
121 | + foreach ($updateParams as $key => $val) { |
|
122 | + $updateSql .= " WHERE $key = $val"; |
|
123 | + } |
|
124 | 124 | |
125 | - $stmt = $dbConn->prepare($updateSql); |
|
125 | + $stmt = $dbConn->prepare($updateSql); |
|
126 | 126 | |
127 | - $boolResponse = $stmt->execute(); |
|
127 | + $boolResponse = $stmt->execute(); |
|
128 | 128 | |
129 | - if ($boolResponse) { |
|
130 | - return true; |
|
131 | - } |
|
129 | + if ($boolResponse) { |
|
130 | + return true; |
|
131 | + } |
|
132 | 132 | |
133 | - return false; |
|
134 | - } |
|
133 | + return false; |
|
134 | + } |
|
135 | 135 | |
136 | 136 | /** |
137 | 137 | * This method retrieves record from a table. |
@@ -142,26 +142,26 @@ discard block |
||
142 | 142 | */ |
143 | 143 | public static function read($id, $tableName, $dbConn = null) |
144 | 144 | { |
145 | - $tableData = []; |
|
145 | + $tableData = []; |
|
146 | 146 | |
147 | - if (is_null($dbConn)) { |
|
148 | - $dbConn = new DatabaseConnection(); |
|
149 | - } |
|
147 | + if (is_null($dbConn)) { |
|
148 | + $dbConn = new DatabaseConnection(); |
|
149 | + } |
|
150 | 150 | |
151 | - $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
151 | + $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
152 | 152 | |
153 | - $stmt = $dbConn->prepare($sql); |
|
154 | - $stmt->bindValue(':table', $tableName); |
|
155 | - $stmt->bindValue(':id', $id); |
|
156 | - $stmt->execute(); |
|
153 | + $stmt = $dbConn->prepare($sql); |
|
154 | + $stmt->bindValue(':table', $tableName); |
|
155 | + $stmt->bindValue(':id', $id); |
|
156 | + $stmt->execute(); |
|
157 | 157 | |
158 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
158 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
159 | 159 | |
160 | - foreach ($results as $result) { |
|
161 | - array_push($tableData, $result); |
|
162 | - } |
|
160 | + foreach ($results as $result) { |
|
161 | + array_push($tableData, $result); |
|
162 | + } |
|
163 | 163 | |
164 | - return $tableData; |
|
164 | + return $tableData; |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -173,19 +173,19 @@ discard block |
||
173 | 173 | */ |
174 | 174 | public static function delete($id, $tableName, $dbConn = null) |
175 | 175 | { |
176 | - if (is_null($dbConn)) { |
|
177 | - $dbConn = new DatabaseConnection(); |
|
178 | - } |
|
176 | + if (is_null($dbConn)) { |
|
177 | + $dbConn = new DatabaseConnection(); |
|
178 | + } |
|
179 | 179 | |
180 | - $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
180 | + $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
181 | 181 | |
182 | - $boolResponse = $dbConn->exec($sql); |
|
182 | + $boolResponse = $dbConn->exec($sql); |
|
183 | 183 | |
184 | - if ($boolResponse) { |
|
185 | - return true; |
|
186 | - } |
|
184 | + if ($boolResponse) { |
|
185 | + return true; |
|
186 | + } |
|
187 | 187 | |
188 | - throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
188 | + throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
@@ -198,15 +198,15 @@ discard block |
||
198 | 198 | */ |
199 | 199 | public static function filterClassAttributes(array $tableColumn, array $userSetterArray) |
200 | 200 | { |
201 | - $unexpectedFields = []; |
|
201 | + $unexpectedFields = []; |
|
202 | 202 | |
203 | - foreach ($userSetterArray as $key => $val) { |
|
204 | - if (!in_array($key, $tableColumn)) { |
|
205 | - $unexpectedFields[] = $key; |
|
206 | - } |
|
207 | - } |
|
203 | + foreach ($userSetterArray as $key => $val) { |
|
204 | + if (!in_array($key, $tableColumn)) { |
|
205 | + $unexpectedFields[] = $key; |
|
206 | + } |
|
207 | + } |
|
208 | 208 | |
209 | - return $unexpectedFields; |
|
209 | + return $unexpectedFields; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -218,13 +218,13 @@ discard block |
||
218 | 218 | */ |
219 | 219 | public function prepareUpdateQuery($sql) |
220 | 220 | { |
221 | - $splittedQuery = explode(',', $sql); |
|
221 | + $splittedQuery = explode(',', $sql); |
|
222 | 222 | |
223 | - array_pop($splittedQuery); |
|
223 | + array_pop($splittedQuery); |
|
224 | 224 | |
225 | - $mergeData = implode(',', $splittedQuery); |
|
225 | + $mergeData = implode(',', $splittedQuery); |
|
226 | 226 | |
227 | - return $mergeData; |
|
227 | + return $mergeData; |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -238,26 +238,26 @@ discard block |
||
238 | 238 | */ |
239 | 239 | public function findAndWhere($params, $tableName, $dbConn = null) |
240 | 240 | { |
241 | - if (is_null($dbConn)) { |
|
242 | - $dbConn = $this->dbConnection; |
|
243 | - } |
|
241 | + if (is_null($dbConn)) { |
|
242 | + $dbConn = $this->dbConnection; |
|
243 | + } |
|
244 | 244 | |
245 | - if (is_array($params) && !empty($params)) { |
|
246 | - $sql = 'SELECT * FROM '.$tableName; |
|
245 | + if (is_array($params) && !empty($params)) { |
|
246 | + $sql = 'SELECT * FROM '.$tableName; |
|
247 | 247 | |
248 | - foreach ($params as $key => $val) { |
|
249 | - $sql .= " WHERE `$key` = '$val'"; |
|
250 | - } |
|
248 | + foreach ($params as $key => $val) { |
|
249 | + $sql .= " WHERE `$key` = '$val'"; |
|
250 | + } |
|
251 | 251 | |
252 | - $statement = $dbConn->prepare($sql); |
|
253 | - $statement->execute(); |
|
252 | + $statement = $dbConn->prepare($sql); |
|
253 | + $statement->execute(); |
|
254 | 254 | |
255 | - $returnedRowNumbers = $statement->rowCount(); |
|
255 | + $returnedRowNumbers = $statement->rowCount(); |
|
256 | 256 | |
257 | - return $returnedRowNumbers ? true : false; |
|
258 | - } |
|
257 | + return $returnedRowNumbers ? true : false; |
|
258 | + } |
|
259 | 259 | |
260 | - throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array'); |
|
260 | + throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array'); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | /** |
@@ -270,24 +270,24 @@ discard block |
||
270 | 270 | */ |
271 | 271 | public function getColumnNames($table, $dbConn = null) |
272 | 272 | { |
273 | - $tableFields = []; |
|
273 | + $tableFields = []; |
|
274 | 274 | |
275 | - if (is_null($dbConn)) { |
|
276 | - $dbConn = $this->dbConnection; |
|
277 | - } |
|
275 | + if (is_null($dbConn)) { |
|
276 | + $dbConn = $this->dbConnection; |
|
277 | + } |
|
278 | 278 | |
279 | - $sql = 'SHOW COLUMNS FROM '.$table; |
|
279 | + $sql = 'SHOW COLUMNS FROM '.$table; |
|
280 | 280 | |
281 | - $stmt = $dbConn->prepare($sql); |
|
282 | - $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
283 | - $stmt->execute(); |
|
281 | + $stmt = $dbConn->prepare($sql); |
|
282 | + $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
283 | + $stmt->execute(); |
|
284 | 284 | |
285 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
285 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
286 | 286 | |
287 | - foreach ($results as $result) { |
|
288 | - array_push($tableFields, $result['Field']); |
|
289 | - } |
|
287 | + foreach ($results as $result) { |
|
288 | + array_push($tableFields, $result['Field']); |
|
289 | + } |
|
290 | 290 | |
291 | - return $tableFields; |
|
291 | + return $tableFields; |
|
292 | 292 | } |
293 | 293 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @throws NoArgumentPassedToFunctionException |
122 | 122 | * |
123 | - * @return object |
|
123 | + * @return BaseModel |
|
124 | 124 | */ |
125 | 125 | public static function find($id) |
126 | 126 | { |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | * This method return the current class name |
172 | 172 | * $params void. |
173 | 173 | * |
174 | - * @return classname |
|
174 | + * @return string|false |
|
175 | 175 | */ |
176 | 176 | public static function getClassName() |
177 | 177 | { |
@@ -8,164 +8,164 @@ discard block |
||
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() |
|
88 | - { |
|
89 | - $boolCommit = false; |
|
90 | - |
|
91 | - if ($this->properties['id']) { |
|
92 | - $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
93 | - |
|
94 | - if ($this->checkIfRecordIsEmpty($allData)) { |
|
95 | - $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
96 | - |
|
97 | - if ($boolCommit) { |
|
98 | - return true; |
|
99 | - } |
|
100 | - |
|
101 | - throw NoRecordUpdateException::create('Record not updated successfully'); |
|
102 | - } |
|
103 | - |
|
104 | - throw EmptyArrayException::create("Value passed didn't match any record"); |
|
105 | - } |
|
106 | - |
|
107 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
108 | - |
|
109 | - if ($boolCommit) { |
|
110 | - return true; |
|
111 | - } |
|
112 | - |
|
113 | - throw NoRecordInsertionException::create('Record not created successfully'); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * This method find a record by id. |
|
118 | - * |
|
119 | - * @params int id |
|
120 | - * |
|
121 | - * @throws NoArgumentPassedToFunctionException |
|
122 | - * |
|
123 | - * @return object |
|
124 | - */ |
|
125 | - public static function find($id) |
|
126 | - { |
|
127 | - $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
128 | - if ($num_args == 0 || $num_args > 1) { |
|
129 | - throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
130 | - } |
|
131 | - |
|
132 | - if ($id == '') { |
|
133 | - throw NullArgumentPassedToFunctionException::create('This function expect a value'); |
|
134 | - } |
|
135 | - |
|
136 | - $staticFindInstance = new static(); |
|
137 | - $staticFindInstance->id = $id == '' ? false : $id; |
|
138 | - |
|
139 | - return $staticFindInstance; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * This method delete a row from the table by the row id. |
|
144 | - * |
|
145 | - * @params int id |
|
146 | - * |
|
147 | - * @throws NoRecordDeletionException; |
|
148 | - * |
|
149 | - * @return bool true or false |
|
150 | - */ |
|
151 | - public static function destroy($id) |
|
152 | - { |
|
153 | - $boolDeleted = false; |
|
154 | - |
|
155 | - $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
156 | - |
|
157 | - if ($num_args == 0 || $num_args > 1) { |
|
158 | - throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
159 | - } |
|
160 | - |
|
161 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
162 | - |
|
163 | - if ($boolDeleted) { |
|
164 | - return true; |
|
165 | - } |
|
166 | - |
|
167 | - throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
168 | - } |
|
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() |
|
88 | + { |
|
89 | + $boolCommit = false; |
|
90 | + |
|
91 | + if ($this->properties['id']) { |
|
92 | + $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
93 | + |
|
94 | + if ($this->checkIfRecordIsEmpty($allData)) { |
|
95 | + $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
96 | + |
|
97 | + if ($boolCommit) { |
|
98 | + return true; |
|
99 | + } |
|
100 | + |
|
101 | + throw NoRecordUpdateException::create('Record not updated successfully'); |
|
102 | + } |
|
103 | + |
|
104 | + throw EmptyArrayException::create("Value passed didn't match any record"); |
|
105 | + } |
|
106 | + |
|
107 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
108 | + |
|
109 | + if ($boolCommit) { |
|
110 | + return true; |
|
111 | + } |
|
112 | + |
|
113 | + throw NoRecordInsertionException::create('Record not created successfully'); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * This method find a record by id. |
|
118 | + * |
|
119 | + * @params int id |
|
120 | + * |
|
121 | + * @throws NoArgumentPassedToFunctionException |
|
122 | + * |
|
123 | + * @return object |
|
124 | + */ |
|
125 | + public static function find($id) |
|
126 | + { |
|
127 | + $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
128 | + if ($num_args == 0 || $num_args > 1) { |
|
129 | + throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
130 | + } |
|
131 | + |
|
132 | + if ($id == '') { |
|
133 | + throw NullArgumentPassedToFunctionException::create('This function expect a value'); |
|
134 | + } |
|
135 | + |
|
136 | + $staticFindInstance = new static(); |
|
137 | + $staticFindInstance->id = $id == '' ? false : $id; |
|
138 | + |
|
139 | + return $staticFindInstance; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * This method delete a row from the table by the row id. |
|
144 | + * |
|
145 | + * @params int id |
|
146 | + * |
|
147 | + * @throws NoRecordDeletionException; |
|
148 | + * |
|
149 | + * @return bool true or false |
|
150 | + */ |
|
151 | + public static function destroy($id) |
|
152 | + { |
|
153 | + $boolDeleted = false; |
|
154 | + |
|
155 | + $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
156 | + |
|
157 | + if ($num_args == 0 || $num_args > 1) { |
|
158 | + throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
159 | + } |
|
160 | + |
|
161 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
162 | + |
|
163 | + if ($boolDeleted) { |
|
164 | + return true; |
|
165 | + } |
|
166 | + |
|
167 | + throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
168 | + } |
|
169 | 169 | |
170 | 170 | /** |
171 | 171 | * This method return the current class name |
@@ -175,26 +175,26 @@ discard block |
||
175 | 175 | */ |
176 | 176 | public static function getClassName() |
177 | 177 | { |
178 | - $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
178 | + $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
179 | 179 | |
180 | - $className = end($tableName); |
|
180 | + $className = end($tableName); |
|
181 | 181 | |
182 | - return self::pluralize(strtolower($className)); |
|
182 | + return self::pluralize(strtolower($className)); |
|
183 | 183 | } |
184 | 184 | |
185 | - /** |
|
186 | - * This method check if the argument passed to this function is an array. |
|
187 | - * |
|
188 | - * @param $arrayOfRecord |
|
189 | - * |
|
190 | - * @return bool |
|
191 | - */ |
|
192 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
193 | - { |
|
194 | - if (count($arrayOfRecord) > 0) { |
|
195 | - return true; |
|
196 | - } |
|
197 | - |
|
198 | - return false; |
|
199 | - } |
|
185 | + /** |
|
186 | + * This method check if the argument passed to this function is an array. |
|
187 | + * |
|
188 | + * @param $arrayOfRecord |
|
189 | + * |
|
190 | + * @return bool |
|
191 | + */ |
|
192 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
193 | + { |
|
194 | + if (count($arrayOfRecord) > 0) { |
|
195 | + return true; |
|
196 | + } |
|
197 | + |
|
198 | + return false; |
|
199 | + } |
|
200 | 200 | } |
@@ -10,10 +10,10 @@ |
||
10 | 10 | |
11 | 11 | class TableFieldUndefinedException extends Exception |
12 | 12 | { |
13 | - public static function create($unExpectedFields, $message) |
|
14 | - { |
|
15 | - $fields = implode(', ', $unExpectedFields); |
|
13 | + public static function create($unExpectedFields, $message) |
|
14 | + { |
|
15 | + $fields = implode(', ', $unExpectedFields); |
|
16 | 16 | |
17 | - return new static($fields.' '.$message); |
|
18 | - } |
|
17 | + return new static($fields.' '.$message); |
|
18 | + } |
|
19 | 19 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NullArgumentPassedToFunctionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static ($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static ($message); |
|
16 | + } |
|
17 | 17 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordDeletionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static($message); |
|
16 | + } |
|
17 | 17 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordInsertionException extends Exception |
12 | 12 | { |
13 | - public static function create($mesaage) |
|
14 | - { |
|
15 | - return new static($mesaage); |
|
16 | - } |
|
13 | + public static function create($mesaage) |
|
14 | + { |
|
15 | + return new static($mesaage); |
|
16 | + } |
|
17 | 17 | } |
@@ -37,62 +37,62 @@ |
||
37 | 37 | |
38 | 38 | trait Inflector |
39 | 39 | { |
40 | - /** |
|
41 | - * Pluralizes English nouns. |
|
42 | - * |
|
43 | - * @static |
|
44 | - * |
|
45 | - * @param string $word English noun to pluralize |
|
46 | - * |
|
47 | - * @return string Plural noun |
|
48 | - */ |
|
49 | - public static function pluralize($word) |
|
50 | - { |
|
51 | - $plural = [ |
|
52 | - '/(quiz)$/i' => '$1zes', |
|
53 | - '/^(ox)$/i' => '$1en', |
|
54 | - '/([m|l])ouse$/i' => '$1ice', |
|
55 | - '/(matr|vert|ind)ix|ex$/i' => '$1ices', |
|
56 | - '/(x|ch|ss|sh)$/i' => '$1es', |
|
57 | - '/([^aeiouy]|qu)y$/i' => '$1ies', |
|
58 | - '/(hive)$/i' => '$1s', |
|
59 | - '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves', |
|
60 | - '/(shea|lea|loa|thie)f$/i' => '$1ves', |
|
61 | - '/sis$/i' => 'ses', |
|
62 | - '/([ti])um$/i' => '$1a', |
|
63 | - '/(tomat|potat|ech|her|vet)o$/i' => '$1oes', |
|
64 | - '/(bu)s$/i' => '$1ses', |
|
65 | - '/(alias)$/i' => '$1es', |
|
66 | - '/(octop)us$/i' => '$1i', |
|
67 | - '/(ax|test)is$/i' => '$1es', |
|
68 | - '/(us)$/i' => '$1es', |
|
69 | - '/s$/i' => 's', |
|
70 | - '/$/' => 's', |
|
71 | - ]; |
|
72 | - $uncountable = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep']; |
|
73 | - $irregular = [ |
|
74 | - 'person' => 'people', |
|
75 | - 'man' => 'men', |
|
76 | - 'child' => 'children', |
|
77 | - 'sex' => 'sexes', |
|
78 | - 'move' => 'moves', ]; |
|
79 | - $lowercased_word = strtolower($word); |
|
80 | - foreach ($uncountable as $_uncountable) { |
|
81 | - if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
82 | - return $word; |
|
83 | - } |
|
84 | - } |
|
85 | - foreach ($irregular as $_plural => $_singular) { |
|
86 | - if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
87 | - return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
88 | - } |
|
89 | - } |
|
90 | - foreach ($plural as $rule => $replacement) { |
|
91 | - if (preg_match($rule, $word)) { |
|
92 | - return preg_replace($rule, $replacement, $word); |
|
93 | - } |
|
94 | - } |
|
40 | + /** |
|
41 | + * Pluralizes English nouns. |
|
42 | + * |
|
43 | + * @static |
|
44 | + * |
|
45 | + * @param string $word English noun to pluralize |
|
46 | + * |
|
47 | + * @return string Plural noun |
|
48 | + */ |
|
49 | + public static function pluralize($word) |
|
50 | + { |
|
51 | + $plural = [ |
|
52 | + '/(quiz)$/i' => '$1zes', |
|
53 | + '/^(ox)$/i' => '$1en', |
|
54 | + '/([m|l])ouse$/i' => '$1ice', |
|
55 | + '/(matr|vert|ind)ix|ex$/i' => '$1ices', |
|
56 | + '/(x|ch|ss|sh)$/i' => '$1es', |
|
57 | + '/([^aeiouy]|qu)y$/i' => '$1ies', |
|
58 | + '/(hive)$/i' => '$1s', |
|
59 | + '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves', |
|
60 | + '/(shea|lea|loa|thie)f$/i' => '$1ves', |
|
61 | + '/sis$/i' => 'ses', |
|
62 | + '/([ti])um$/i' => '$1a', |
|
63 | + '/(tomat|potat|ech|her|vet)o$/i' => '$1oes', |
|
64 | + '/(bu)s$/i' => '$1ses', |
|
65 | + '/(alias)$/i' => '$1es', |
|
66 | + '/(octop)us$/i' => '$1i', |
|
67 | + '/(ax|test)is$/i' => '$1es', |
|
68 | + '/(us)$/i' => '$1es', |
|
69 | + '/s$/i' => 's', |
|
70 | + '/$/' => 's', |
|
71 | + ]; |
|
72 | + $uncountable = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep']; |
|
73 | + $irregular = [ |
|
74 | + 'person' => 'people', |
|
75 | + 'man' => 'men', |
|
76 | + 'child' => 'children', |
|
77 | + 'sex' => 'sexes', |
|
78 | + 'move' => 'moves', ]; |
|
79 | + $lowercased_word = strtolower($word); |
|
80 | + foreach ($uncountable as $_uncountable) { |
|
81 | + if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
82 | + return $word; |
|
83 | + } |
|
84 | + } |
|
85 | + foreach ($irregular as $_plural => $_singular) { |
|
86 | + if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
87 | + return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
88 | + } |
|
89 | + } |
|
90 | + foreach ($plural as $rule => $replacement) { |
|
91 | + if (preg_match($rule, $word)) { |
|
92 | + return preg_replace($rule, $replacement, $word); |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | - return false; |
|
97 | - } |
|
96 | + return false; |
|
97 | + } |
|
98 | 98 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | 'move' => 'moves', ]; |
79 | 79 | $lowercased_word = strtolower($word); |
80 | 80 | foreach ($uncountable as $_uncountable) { |
81 | - if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
81 | + if (substr($lowercased_word, (-1*strlen($_uncountable))) == $_uncountable) { |
|
82 | 82 | return $word; |
83 | 83 | } |
84 | 84 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordDeletionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static($message); |
|
16 | + } |
|
17 | 17 | } |