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