|
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
|
42 |
|
public function __construct($dbConn) |
|
29
|
|
|
{ |
|
30
|
42 |
|
$this->tableName = self::getClassName(); |
|
31
|
3 |
|
$this->dataBaseQuery = new DataBaseQuery($dbConn); |
|
32
|
|
|
$this->arrayField['id'] = 0; |
|
33
|
42 |
|
} |
|
34
|
|
|
|
|
35
|
42 |
|
/** |
|
36
|
42 |
|
* The magic setter method. |
|
37
|
42 |
|
* |
|
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
|
42 |
|
|
|
48
|
|
|
/** |
|
49
|
42 |
|
* The magic getter method. |
|
50
|
42 |
|
* |
|
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
|
|
|
public static function getAll($dbConn) |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
$sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn); |
|
71
|
|
|
|
|
72
|
6 |
|
if (count($sqlData) > 0) { |
|
73
|
|
|
return $sqlData; |
|
74
|
6 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
self::throwNoDataFoundException(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function throwNoDataFoundException() |
|
80
|
|
|
{ |
|
81
|
|
|
$message = "oops, no data found in the database"; |
|
82
|
|
|
throw new NoDataFoundException($message); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* This method either create or update record in a database table |
|
87
|
|
|
* by calling either the read method or create method in the |
|
88
|
|
|
* DataBaseQuery class. |
|
89
|
|
|
* |
|
90
|
2 |
|
* @throws NoRecordUpdateException |
|
91
|
|
|
* @throws EmptyArrayException |
|
92
|
|
|
* @throws NoRecordCreatedException |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool true or false; |
|
95
|
|
|
*/ |
|
96
|
|
|
public function save($dbConn) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->arrayField['id']) { |
|
99
|
2 |
|
$sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn); |
|
100
|
|
|
|
|
101
|
|
|
if ($this->checkIfRecordIsEmpty($sqlData)) { |
|
102
|
|
|
$boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn); |
|
103
|
|
|
|
|
104
|
|
|
if ($boolCommit) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->throwNoRecordUpdatedException(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->throwEmptyArrayException(); |
|
112
|
|
|
} else { |
|
113
|
|
|
|
|
114
|
|
|
$boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn); |
|
115
|
|
|
|
|
116
|
|
|
if ($boolCommit) { |
|
|
|
|
|
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->throwNoRecordCreatedException(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function throwNoRecordUpdatedException() |
|
126
|
|
|
{ |
|
127
|
|
|
$message = "oops, your record did not update succesfully"; |
|
128
|
|
|
throw new NoRecordUpdatedException($message); |
|
129
|
9 |
|
} |
|
130
|
|
|
|
|
131
|
9 |
|
public function throwEmptyArrayException() |
|
132
|
|
|
{ |
|
133
|
9 |
|
$message = "data passed didn't match any record"; |
|
134
|
3 |
|
throw new EmptyArrayException($message); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
6 |
|
public function throwNoRecordCreatedException() |
|
138
|
3 |
|
{ |
|
139
|
|
|
$message = "oops,your record did not create succesfully"; |
|
140
|
|
|
throw new NoRecordCreatedException($message); |
|
141
|
3 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* This method find a record by id. |
|
145
|
|
|
* |
|
146
|
|
|
* @param $id |
|
147
|
|
|
* |
|
148
|
|
|
* @throws ArgumentNumberIncorrectException |
|
149
|
|
|
* @throws ArgumentNotFoundException |
|
150
|
|
|
* |
|
151
|
|
|
* @return object |
|
152
|
|
|
*/ |
|
153
|
|
|
public static function findById($id) |
|
154
|
|
|
{ |
|
155
|
|
|
$numArgs = func_num_args(); |
|
156
|
|
|
|
|
157
|
|
|
if ($numArgs > 1) { |
|
158
|
|
|
throw new ArgumentNumberIncorrectException('Please input just one Argument'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if ($id == '') { |
|
162
|
|
|
throw new ArgumentNotFoundException('No Argument found, please input an argument'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$staticFindInstance = new static(); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$staticFindInstance->id = $id; |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
return $staticFindInstance; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* This method find a record by id and returns |
|
174
|
9 |
|
* all the data present in the id. |
|
175
|
|
|
* |
|
176
|
9 |
|
* |
|
177
|
9 |
|
* @return associative array |
|
178
|
3 |
|
*/ |
|
179
|
|
|
public function getById() |
|
180
|
|
|
{ |
|
181
|
6 |
|
if ($this->arrayField['id']) { |
|
182
|
3 |
|
$sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
return $sqlData; |
|
185
|
3 |
|
} |
|
186
|
|
|
} |
|
187
|
3 |
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* This method delete a row from the table by the row id. |
|
190
|
42 |
|
* |
|
191
|
|
|
* @param $id |
|
192
|
42 |
|
* |
|
193
|
|
|
* @throws ArgumentNumberIncorrectException; |
|
194
|
42 |
|
* @throws ArgumentNotFoundException; |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool true |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function destroy($id, $dbConn) |
|
199
|
|
|
{ |
|
200
|
|
|
$numArgs = func_num_args(); |
|
201
|
|
|
if ($numArgs > 2) { |
|
202
|
|
|
throw new ArgumentNumberIncorrectException('Please input just one Argument'); |
|
203
|
|
|
} |
|
204
|
6 |
|
|
|
205
|
|
|
if ($numArgs == ' ') { |
|
206
|
6 |
|
throw new ArgumentNotFoundException('No Argument found, please input an argument'); |
|
207
|
3 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
DataBaseQuery::delete($id, self::getClassName(), $dbConn); |
|
210
|
3 |
|
|
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public static function getClassName() |
|
215
|
|
|
{ |
|
216
|
|
|
$tableName = explode('\\', get_called_class()); |
|
217
|
|
|
|
|
218
|
|
|
return Inflector::pluralize(strtolower(end($tableName))); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* This method check if the argument passed to this function is an array. |
|
223
|
|
|
* |
|
224
|
|
|
* @param $arrayOfRecord |
|
225
|
|
|
* |
|
226
|
|
|
* @return bool true |
|
227
|
|
|
*/ |
|
228
|
|
|
public function checkIfRecordIsEmpty($arrayOfRecord) |
|
229
|
|
|
{ |
|
230
|
|
|
if (count($arrayOfRecord) > 0) { |
|
231
|
|
|
return true; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
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.