@@ -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 | } |
@@ -12,71 +12,71 @@ |
||
12 | 12 | |
13 | 13 | class DatabaseConnection extends PDO |
14 | 14 | { |
15 | - private $databaseName; |
|
16 | - private $databaseHost; |
|
17 | - private $databaseDriver; |
|
18 | - private $databaseUsername; |
|
19 | - private $databasePassword; |
|
15 | + private $databaseName; |
|
16 | + private $databaseHost; |
|
17 | + private $databaseDriver; |
|
18 | + private $databaseUsername; |
|
19 | + private $databasePassword; |
|
20 | 20 | |
21 | - public function __construct() |
|
22 | - { |
|
23 | - $this->loadEnv(); // load the environment variables |
|
21 | + public function __construct() |
|
22 | + { |
|
23 | + $this->loadEnv(); // load the environment variables |
|
24 | 24 | |
25 | - $this->databaseName = getenv('databaseName'); |
|
26 | - $this->databaseHost = getenv('databaseHost'); |
|
27 | - $this->databaseDriver = getenv('databaseDriver'); |
|
28 | - $this->databaseUsername = getenv('databaseUsername'); |
|
29 | - $this->databasePassword = getenv('databasePassword'); |
|
25 | + $this->databaseName = getenv('databaseName'); |
|
26 | + $this->databaseHost = getenv('databaseHost'); |
|
27 | + $this->databaseDriver = getenv('databaseDriver'); |
|
28 | + $this->databaseUsername = getenv('databaseUsername'); |
|
29 | + $this->databasePassword = getenv('databasePassword'); |
|
30 | 30 | |
31 | - try { |
|
32 | - $options = [ |
|
33 | - PDO::ATTR_PERSISTENT => true, |
|
34 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
35 | - ]; |
|
31 | + try { |
|
32 | + $options = [ |
|
33 | + PDO::ATTR_PERSISTENT => true, |
|
34 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
35 | + ]; |
|
36 | 36 | |
37 | - parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
38 | - } catch (PDOException $e) { |
|
39 | - return $e->getMessage(); |
|
40 | - } |
|
41 | - } |
|
37 | + parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
38 | + } catch (PDOException $e) { |
|
39 | + return $e->getMessage(); |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * This method determines the driver to be used for appropriate database server. |
|
45 | - * |
|
46 | - * @params void |
|
47 | - * |
|
48 | - * @return string dsn |
|
49 | - */ |
|
50 | - public function getDatabaseDriver() |
|
51 | - { |
|
52 | - $dsn = ''; |
|
43 | + /** |
|
44 | + * This method determines the driver to be used for appropriate database server. |
|
45 | + * |
|
46 | + * @params void |
|
47 | + * |
|
48 | + * @return string dsn |
|
49 | + */ |
|
50 | + public function getDatabaseDriver() |
|
51 | + { |
|
52 | + $dsn = ''; |
|
53 | 53 | |
54 | - switch ($this->databaseDriver) { |
|
55 | - case 'mysql': |
|
56 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN |
|
57 | - break; |
|
58 | - case 'sqlite': |
|
59 | - $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
60 | - break; |
|
61 | - case 'pgsql': |
|
62 | - $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
63 | - break; |
|
64 | - default: |
|
65 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
66 | - break; |
|
67 | - } |
|
54 | + switch ($this->databaseDriver) { |
|
55 | + case 'mysql': |
|
56 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN |
|
57 | + break; |
|
58 | + case 'sqlite': |
|
59 | + $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
60 | + break; |
|
61 | + case 'pgsql': |
|
62 | + $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
63 | + break; |
|
64 | + default: |
|
65 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
66 | + break; |
|
67 | + } |
|
68 | 68 | |
69 | - return $dsn; |
|
70 | - } |
|
69 | + return $dsn; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Load Dotenv to grant getenv() access to environment variables in .env file. |
|
74 | - */ |
|
75 | - public function loadEnv() |
|
76 | - { |
|
77 | - if (! getenv('APP_ENV')) { |
|
78 | - $dotenv = new Dotenv(__DIR__.'/../../'); |
|
79 | - $dotenv->load(); |
|
80 | - } |
|
81 | - } |
|
72 | + /** |
|
73 | + * Load Dotenv to grant getenv() access to environment variables in .env file. |
|
74 | + */ |
|
75 | + public function loadEnv() |
|
76 | + { |
|
77 | + if (! getenv('APP_ENV')) { |
|
78 | + $dotenv = new Dotenv(__DIR__.'/../../'); |
|
79 | + $dotenv->load(); |
|
80 | + } |
|
81 | + } |
|
82 | 82 | } |
@@ -74,7 +74,7 @@ |
||
74 | 74 | */ |
75 | 75 | public function loadEnv() |
76 | 76 | { |
77 | - if (! getenv('APP_ENV')) { |
|
77 | + if (!getenv('APP_ENV')) { |
|
78 | 78 | $dotenv = new Dotenv(__DIR__.'/../../'); |
79 | 79 | $dotenv->load(); |
80 | 80 | } |