@@ -20,156 +20,156 @@ discard block |
||
20 | 20 | |
21 | 21 | class BaseModel implements BaseModelInterface |
22 | 22 | { |
23 | - // Inject the inflector trait |
|
24 | - use Inflector; |
|
23 | + // Inject the inflector trait |
|
24 | + use Inflector; |
|
25 | 25 | |
26 | - // Private variable that contains instance of database |
|
27 | - protected $databaseModel; |
|
26 | + // Private variable that contains instance of database |
|
27 | + protected $databaseModel; |
|
28 | 28 | |
29 | - // Class variable holding class name pluralized |
|
30 | - protected $tableName; |
|
29 | + // Class variable holding class name pluralized |
|
30 | + protected $tableName; |
|
31 | 31 | |
32 | - // Properties will later contain key, value pairs from the magic setter, getter methods |
|
33 | - protected $properties = []; |
|
32 | + // Properties will later contain key, value pairs from the magic setter, getter methods |
|
33 | + protected $properties = []; |
|
34 | 34 | |
35 | - public function __construct() |
|
36 | - { |
|
37 | - $this->tableName = $this->getClassName(); |
|
35 | + public function __construct() |
|
36 | + { |
|
37 | + $this->tableName = $this->getClassName(); |
|
38 | 38 | |
39 | - $this->databaseModel = new DatabaseHandler($this->tableName); |
|
39 | + $this->databaseModel = new DatabaseHandler($this->tableName); |
|
40 | 40 | |
41 | - $this->properties['id'] = 0; |
|
42 | - } |
|
41 | + $this->properties['id'] = 0; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * The magic getter method |
|
46 | - * @params key |
|
47 | - * @return array key |
|
48 | - */ |
|
49 | - public function __get($key) |
|
50 | - { |
|
51 | - $this->properties[$key]; |
|
52 | - |
|
53 | - } |
|
44 | + /** |
|
45 | + * The magic getter method |
|
46 | + * @params key |
|
47 | + * @return array key |
|
48 | + */ |
|
49 | + public function __get($key) |
|
50 | + { |
|
51 | + $this->properties[$key]; |
|
52 | + |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * The magic setter method |
|
57 | - * @params property, key |
|
58 | - * @return array associative array properties |
|
59 | - */ |
|
60 | - public function __set($property, $value) |
|
61 | - { |
|
62 | - $this->properties[$property] = $value; |
|
63 | - |
|
64 | - } |
|
55 | + /** |
|
56 | + * The magic setter method |
|
57 | + * @params property, key |
|
58 | + * @return array associative array properties |
|
59 | + */ |
|
60 | + public function __set($property, $value) |
|
61 | + { |
|
62 | + $this->properties[$property] = $value; |
|
63 | + |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * This method gets all the record from a particular table |
|
68 | - * @params void |
|
69 | - * @return associative array |
|
70 | - * @throws NoRecordFoundException |
|
71 | - */ |
|
72 | - public static function getAll() |
|
73 | - { |
|
74 | - $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
66 | + /** |
|
67 | + * This method gets all the record from a particular table |
|
68 | + * @params void |
|
69 | + * @return associative array |
|
70 | + * @throws NoRecordFoundException |
|
71 | + */ |
|
72 | + public static function getAll() |
|
73 | + { |
|
74 | + $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
75 | 75 | |
76 | - if (count($allData) > 0) { |
|
77 | - return $allData; |
|
76 | + if (count($allData) > 0) { |
|
77 | + return $allData; |
|
78 | 78 | |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - throw NoRecordFoundException::create("There is no record to display"); |
|
81 | + throw NoRecordFoundException::create("There is no record to display"); |
|
82 | 82 | |
83 | - } |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * This method create or update record in a database table |
|
87 | - * @params void |
|
88 | - * @return bool true or false; |
|
89 | - * @throws EmptyArrayException |
|
90 | - * @throws NoRecordInsertionException |
|
91 | - * @throws NoRecordUpdateException |
|
92 | - */ |
|
93 | - public function save() |
|
94 | - { |
|
95 | - $boolCommit = false; |
|
96 | - |
|
97 | - if ($this->properties['id']) { |
|
98 | - $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
99 | - |
|
100 | - if ($this->checkIfRecordIsEmpty($allData)) { |
|
101 | - $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
102 | - |
|
103 | - if ($boolCommit) { |
|
104 | - return true; |
|
105 | - |
|
106 | - } |
|
107 | - |
|
108 | - throw NoRecordUpdateException::create("Record not updated successfully"); |
|
109 | - } |
|
110 | - |
|
111 | - throw EmptyArrayException::create("Value passed didn't match any record"); |
|
112 | - } |
|
113 | - |
|
114 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
115 | - |
|
116 | - if ($boolCommit) { |
|
117 | - return true; |
|
118 | - } |
|
119 | - |
|
120 | - throw NoRecordInsertionException::create("Record not created successfully"); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * This method find a record by id |
|
125 | - * @params int id |
|
126 | - * @return Object |
|
127 | - * @throws NoArgumentPassedToFunctionException |
|
128 | - */ |
|
129 | - public static function find($id) |
|
130 | - { |
|
131 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
132 | - |
|
133 | - if ($num_args == 0 || $num_args > 1) { |
|
134 | - throw NoArgumentPassedToFunctionException::create("Argument missing: only one argument is allowed"); |
|
135 | - } |
|
136 | - |
|
137 | - if ($id == "") { |
|
138 | - throw NullArgumentPassedToFunction::create("This function expect a value"); |
|
139 | - } |
|
140 | - |
|
141 | - $staticFindInstance = new static(); |
|
142 | - $staticFindInstance->id = $id == "" ? false : $id; |
|
143 | - |
|
144 | - return $staticFindInstance; |
|
85 | + /** |
|
86 | + * This method create or update record in a database table |
|
87 | + * @params void |
|
88 | + * @return bool true or false; |
|
89 | + * @throws EmptyArrayException |
|
90 | + * @throws NoRecordInsertionException |
|
91 | + * @throws NoRecordUpdateException |
|
92 | + */ |
|
93 | + public function save() |
|
94 | + { |
|
95 | + $boolCommit = false; |
|
96 | + |
|
97 | + if ($this->properties['id']) { |
|
98 | + $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
99 | + |
|
100 | + if ($this->checkIfRecordIsEmpty($allData)) { |
|
101 | + $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
102 | + |
|
103 | + if ($boolCommit) { |
|
104 | + return true; |
|
105 | + |
|
106 | + } |
|
107 | + |
|
108 | + throw NoRecordUpdateException::create("Record not updated successfully"); |
|
109 | + } |
|
110 | + |
|
111 | + throw EmptyArrayException::create("Value passed didn't match any record"); |
|
112 | + } |
|
113 | + |
|
114 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
115 | + |
|
116 | + if ($boolCommit) { |
|
117 | + return true; |
|
118 | + } |
|
119 | + |
|
120 | + throw NoRecordInsertionException::create("Record not created successfully"); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * This method find a record by id |
|
125 | + * @params int id |
|
126 | + * @return Object |
|
127 | + * @throws NoArgumentPassedToFunctionException |
|
128 | + */ |
|
129 | + public static function find($id) |
|
130 | + { |
|
131 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
132 | + |
|
133 | + if ($num_args == 0 || $num_args > 1) { |
|
134 | + throw NoArgumentPassedToFunctionException::create("Argument missing: only one argument is allowed"); |
|
135 | + } |
|
136 | + |
|
137 | + if ($id == "") { |
|
138 | + throw NullArgumentPassedToFunction::create("This function expect a value"); |
|
139 | + } |
|
140 | + |
|
141 | + $staticFindInstance = new static(); |
|
142 | + $staticFindInstance->id = $id == "" ? false : $id; |
|
143 | + |
|
144 | + return $staticFindInstance; |
|
145 | 145 | |
146 | - } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * This method delete a row from the table by the row id |
|
150 | - * @params int id |
|
151 | - * @return boolean true or false |
|
152 | - * @throws NoRecordDeletionException; |
|
153 | - */ |
|
154 | - public static function destroy($id) |
|
155 | - { |
|
156 | - $boolDeleted = false; |
|
157 | - |
|
158 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
148 | + /** |
|
149 | + * This method delete a row from the table by the row id |
|
150 | + * @params int id |
|
151 | + * @return boolean true or false |
|
152 | + * @throws NoRecordDeletionException; |
|
153 | + */ |
|
154 | + public static function destroy($id) |
|
155 | + { |
|
156 | + $boolDeleted = false; |
|
157 | + |
|
158 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
159 | 159 | |
160 | - if ($num_args == 0 || $num_args > 1) { |
|
161 | - throw NoArgumentPassedToFunctionException::create("Argument missing: only one argument is allowed"); |
|
162 | - } |
|
160 | + if ($num_args == 0 || $num_args > 1) { |
|
161 | + throw NoArgumentPassedToFunctionException::create("Argument missing: only one argument is allowed"); |
|
162 | + } |
|
163 | 163 | |
164 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
164 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
165 | 165 | |
166 | - if ($boolDeleted) { |
|
167 | - return true; |
|
166 | + if ($boolDeleted) { |
|
167 | + return true; |
|
168 | 168 | |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - throw NoRecordDeletionException::create("Record deletion unsuccessful because id does not match any record"); |
|
172 | - } |
|
171 | + throw NoRecordDeletionException::create("Record deletion unsuccessful because id does not match any record"); |
|
172 | + } |
|
173 | 173 | |
174 | 174 | /** |
175 | 175 | * This method return the current class name |
@@ -178,11 +178,11 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public static function getClassName() |
180 | 180 | { |
181 | - $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
181 | + $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
182 | 182 | |
183 | - $className = end($tableName); |
|
183 | + $className = end($tableName); |
|
184 | 184 | |
185 | - return self::pluralize(strtolower($className)); |
|
185 | + return self::pluralize(strtolower($className)); |
|
186 | 186 | |
187 | 187 | } |
188 | 188 | |
@@ -191,14 +191,14 @@ discard block |
||
191 | 191 | * @param $arrayOfRecord |
192 | 192 | * @return bool |
193 | 193 | */ |
194 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
195 | - { |
|
196 | - if (count($arrayOfRecord) > 0) { |
|
197 | - return true; |
|
194 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
195 | + { |
|
196 | + if (count($arrayOfRecord) > 0) { |
|
197 | + return true; |
|
198 | 198 | |
199 | - } |
|
199 | + } |
|
200 | 200 | |
201 | - return false; |
|
202 | - } |
|
201 | + return false; |
|
202 | + } |
|
203 | 203 | |
204 | 204 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function create($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function create($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function create($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function create($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function create($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function create($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function create($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function create($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function create($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function create($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |