1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class DataBaseModel: |
5
|
|
|
* This is an abstract class which stands as a model |
6
|
|
|
* to another class e.g User class which can inherit from |
7
|
|
|
* all its methods. This class stands as a middle man |
8
|
|
|
* between the User class and the DataBaseQuery class. |
9
|
|
|
* |
10
|
|
|
* @author: Raimi Ademola <[email protected]> |
11
|
|
|
* @copyright: 2016 Andela |
12
|
|
|
*/ |
13
|
|
|
namespace Demo; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Inflector\Inflector; |
16
|
|
|
|
17
|
|
|
abstract class DataBaseModel implements DataBaseModelInterface |
18
|
|
|
{ |
19
|
|
|
protected $tableName; |
20
|
|
|
protected $dbConn; |
21
|
|
|
protected $dataBaseQuery; |
22
|
|
|
protected $properties; |
23
|
|
|
protected $arrayField; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is a constructor; a default method that will be called automatically during class instantiation. |
27
|
|
|
*/ |
28
|
54 |
|
public function __construct($dbConn) |
29
|
|
|
{ |
30
|
54 |
|
$this->tableName = self::getClassName(); |
31
|
54 |
|
$this->dataBaseQuery = new DataBaseQuery($dbConn); |
32
|
54 |
|
$this->arrayField['id'] = 0; |
33
|
54 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The magic setter method. |
37
|
|
|
* |
38
|
|
|
* @param $properties |
39
|
|
|
* @param $values |
40
|
|
|
* |
41
|
|
|
* @return array associative array properties |
42
|
|
|
*/ |
43
|
|
|
public function __set($properties, $values) |
44
|
|
|
{ |
45
|
|
|
$this->arrayField[$properties] = $values; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The magic getter method. |
50
|
|
|
* |
51
|
|
|
* @param $properties |
52
|
|
|
* |
53
|
|
|
* @return array key |
54
|
|
|
*/ |
55
|
|
|
public function __get($properties) |
56
|
|
|
{ |
57
|
|
|
return $this->arrayField[$properties]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* This method gets all the record from a particular table |
62
|
|
|
* by accessing the read method from the DataBaseQuery class. |
63
|
|
|
* |
64
|
|
|
* |
65
|
|
|
* @return associative array |
66
|
|
|
*/ |
67
|
6 |
|
public static function getAll($dbConn) |
68
|
|
|
{ |
69
|
6 |
|
$sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn); |
70
|
|
|
|
71
|
3 |
|
if (count($sqlData) > 0) { |
72
|
3 |
|
return $sqlData; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
self::throwNoDataFoundException(); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
public static function throwNoDataFoundException() |
79
|
|
|
{ |
80
|
3 |
|
$message = "oops, no data found in the database"; |
81
|
3 |
|
throw new NoDataFoundException($message); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This method either create or update record in a database table |
86
|
|
|
* by calling either the read method or create method in the |
87
|
|
|
* DataBaseQuery class. |
88
|
|
|
* |
89
|
|
|
* @throws NoRecordUpdateException |
90
|
|
|
* @throws EmptyArrayException |
91
|
|
|
* @throws NoRecordCreatedException |
92
|
|
|
* |
93
|
|
|
* @return bool true or false; |
94
|
|
|
*/ |
95
|
|
|
public function save($dbConn) |
96
|
|
|
{ |
97
|
|
|
if ($this->arrayField['id']) { |
98
|
|
|
$sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn); |
99
|
|
|
|
100
|
|
|
if ($this->checkIfRecordIsEmpty($sqlData)) { |
101
|
|
|
$boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn); |
102
|
|
|
|
103
|
|
|
if ($boolCommit) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->throwNoRecordUpdatedException(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->throwEmptyArrayException(); |
111
|
|
|
} else { |
112
|
|
|
|
113
|
|
|
$boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn); |
114
|
|
|
|
115
|
|
|
if ($boolCommit) { |
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->throwNoRecordCreatedException(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
public function throwNoRecordUpdatedException() |
125
|
|
|
{ |
126
|
3 |
|
$message = "oops, your record did not update succesfully"; |
127
|
3 |
|
throw new NoRecordUpdatedException($message); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
public function throwEmptyArrayException() |
131
|
|
|
{ |
132
|
3 |
|
$message = "data passed didn't match any record"; |
133
|
3 |
|
throw new EmptyArrayException($message); |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
public function throwNoRecordCreatedException() |
137
|
|
|
{ |
138
|
3 |
|
$message = "oops,your record did not create succesfully"; |
139
|
3 |
|
throw new NoRecordCreatedException($message); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* This method find a record by id. |
144
|
|
|
* |
145
|
|
|
* @param $id |
146
|
|
|
* |
147
|
|
|
* @throws ArgumentNumberIncorrectException |
148
|
|
|
* @throws ArgumentNotFoundException |
149
|
|
|
* |
150
|
|
|
* @return object |
151
|
|
|
*/ |
152
|
6 |
|
public static function findById($id) |
153
|
|
|
{ |
154
|
6 |
|
$numArgs = func_num_args(); |
155
|
|
|
|
156
|
6 |
|
if ($numArgs > 1) { |
157
|
3 |
|
throw new ArgumentNumberIncorrectException('Please input just one Argument'); |
158
|
|
|
} |
159
|
|
|
|
160
|
3 |
|
if ($id == '') { |
161
|
3 |
|
throw new ArgumentNotFoundException('No Argument found, please input an argument'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$staticFindInstance = new static(); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$staticFindInstance->id = $id; |
|
|
|
|
167
|
|
|
|
168
|
|
|
return $staticFindInstance; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* This method find a record by id and returns |
173
|
|
|
* all the data present in the id. |
174
|
|
|
* |
175
|
|
|
* |
176
|
|
|
* @return associative array |
177
|
|
|
*/ |
178
|
|
|
public function getById() |
179
|
|
|
{ |
180
|
|
|
if ($this->arrayField['id']) { |
181
|
|
|
$sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn); |
|
|
|
|
182
|
|
|
|
183
|
|
|
if (count($sqlData) > 0) { |
184
|
|
|
return $sqlData; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
self::throwDataEmptyException(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
public function throwDataEmptyException() |
192
|
|
|
{ |
193
|
3 |
|
$message = "oops, no data found in the column"; |
194
|
3 |
|
throw new DataEmptyException($message); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* This method delete a row from the table by the row id. |
199
|
|
|
* |
200
|
|
|
* @param $id |
201
|
|
|
* |
202
|
|
|
* @throws ArgumentNumberIncorrectException; |
203
|
|
|
* @throws ArgumentNotFoundException; |
204
|
|
|
* |
205
|
|
|
* @return bool true |
206
|
|
|
*/ |
207
|
9 |
|
public static function destroy($id, $dbConn) |
208
|
|
|
{ |
209
|
9 |
|
$numArgs = func_num_args(); |
210
|
9 |
|
if ($numArgs > 2) { |
211
|
3 |
|
throw new ArgumentNumberIncorrectException('Please input just one Argument'); |
212
|
|
|
} |
213
|
|
|
|
214
|
6 |
|
if ($numArgs == ' ') { |
215
|
3 |
|
throw new ArgumentNotFoundException('No Argument found, please input an argument'); |
216
|
|
|
} |
217
|
|
|
|
218
|
3 |
|
DataBaseQuery::delete($id, self::getClassName(), $dbConn); |
219
|
|
|
|
220
|
3 |
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
54 |
|
public static function getClassName() |
224
|
|
|
{ |
225
|
54 |
|
$tableName = explode('\\', get_called_class()); |
226
|
|
|
|
227
|
54 |
|
return Inflector::pluralize(strtolower(end($tableName))); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* This method check if the argument passed to this function is an array. |
232
|
|
|
* |
233
|
|
|
* @param $arrayOfRecord |
234
|
|
|
* |
235
|
|
|
* @return bool true |
236
|
|
|
*/ |
237
|
6 |
|
public function checkIfRecordIsEmpty($arrayOfRecord) |
238
|
|
|
{ |
239
|
6 |
|
if (count($arrayOfRecord) > 0) { |
240
|
3 |
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
3 |
|
return false; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.