@@ -10,12 +10,12 @@ |
||
10 | 10 | |
11 | 11 | interface InterfaceBaseClass { |
12 | 12 | |
13 | - /** |
|
14 | - * This method gets all the record from a particular table |
|
15 | - * @params void |
|
16 | - * @return associative array |
|
17 | - */ |
|
18 | - public static function getAll(); |
|
13 | + /** |
|
14 | + * This method gets all the record from a particular table |
|
15 | + * @params void |
|
16 | + * @return associative array |
|
17 | + */ |
|
18 | + public static function getAll(); |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * This method create or update record in a database table |
@@ -10,5 +10,5 @@ |
||
10 | 10 | |
11 | 11 | class Inflector { |
12 | 12 | |
13 | - use Inflector; |
|
13 | + use Inflector; |
|
14 | 14 | } |
@@ -20,150 +20,150 @@ discard block |
||
20 | 20 | |
21 | 21 | class BaseModel implements InterfaceBaseClass |
22 | 22 | { |
23 | - protected $databaseModel; // Private variable that contains instance of database |
|
24 | - protected $tableName; // Class variable holding class name pluralized |
|
25 | - protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
23 | + protected $databaseModel; // Private variable that contains instance of database |
|
24 | + protected $tableName; // Class variable holding class name pluralized |
|
25 | + protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
26 | 26 | |
27 | - use Inflector; // Inject the inflector trait |
|
27 | + use Inflector; // Inject the inflector trait |
|
28 | 28 | |
29 | - public function __construct() |
|
30 | - { |
|
31 | - $this->tableName = $this->getClassName(); |
|
29 | + public function __construct() |
|
30 | + { |
|
31 | + $this->tableName = $this->getClassName(); |
|
32 | 32 | |
33 | - $this->databaseModel = new DatabaseHandler($this->tableName); |
|
33 | + $this->databaseModel = new DatabaseHandler($this->tableName); |
|
34 | 34 | |
35 | - $this->properties['id'] = 0; |
|
36 | - } |
|
35 | + $this->properties['id'] = 0; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * The magic getter method |
|
40 | - * @params key |
|
41 | - * @return array key |
|
42 | - */ |
|
43 | - public function __get($key) |
|
44 | - { |
|
45 | - $this->properties[$key]; |
|
46 | - |
|
47 | - } |
|
38 | + /** |
|
39 | + * The magic getter method |
|
40 | + * @params key |
|
41 | + * @return array key |
|
42 | + */ |
|
43 | + public function __get($key) |
|
44 | + { |
|
45 | + $this->properties[$key]; |
|
46 | + |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * The magic setter method |
|
51 | - * @params property, key |
|
52 | - * @return array associative array properties |
|
53 | - */ |
|
54 | - public function __set($property, $value) |
|
55 | - { |
|
56 | - $this->properties[$property] = $value; |
|
57 | - |
|
58 | - } |
|
49 | + /** |
|
50 | + * The magic setter method |
|
51 | + * @params property, key |
|
52 | + * @return array associative array properties |
|
53 | + */ |
|
54 | + public function __set($property, $value) |
|
55 | + { |
|
56 | + $this->properties[$property] = $value; |
|
57 | + |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * This method gets all the record from a particular table |
|
62 | - * @params void |
|
63 | - * @return associative array |
|
64 | - * @throws NoRecordFoundException |
|
65 | - */ |
|
66 | - public static function getAll() |
|
67 | - { |
|
68 | - $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
60 | + /** |
|
61 | + * This method gets all the record from a particular table |
|
62 | + * @params void |
|
63 | + * @return associative array |
|
64 | + * @throws NoRecordFoundException |
|
65 | + */ |
|
66 | + public static function getAll() |
|
67 | + { |
|
68 | + $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
69 | 69 | |
70 | - if (count($allData) > 0) { |
|
71 | - return $allData; |
|
70 | + if (count($allData) > 0) { |
|
71 | + return $allData; |
|
72 | 72 | |
73 | - } |
|
73 | + } |
|
74 | 74 | |
75 | - throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
75 | + throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
76 | 76 | |
77 | - } |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * This method create or update record in a database table |
|
81 | - * @params void |
|
82 | - * @return bool true or false; |
|
83 | - * @throws EmptyArrayException |
|
84 | - * @throws NoRecordInsertionException |
|
85 | - * @throws NoRecordUpdateException |
|
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 | - |
|
102 | - throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
103 | - } |
|
104 | - |
|
105 | - throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
106 | - } |
|
107 | - |
|
108 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
109 | - |
|
110 | - if ($boolCommit) { |
|
111 | - return true; |
|
112 | - } |
|
113 | - |
|
114 | - throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * This method find a record by id |
|
119 | - * @params int id |
|
120 | - * @return Object |
|
121 | - * @throws NoArgumentPassedToFunctionException |
|
122 | - */ |
|
123 | - public static function find($id) |
|
124 | - { |
|
125 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
126 | - |
|
127 | - if ($num_args == 0 || $num_args > 1) { |
|
128 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
129 | - } |
|
130 | - |
|
131 | - if ($id == "") { |
|
132 | - throw NullArgumentPassedToFunction::checkNullArgumentPassedToFunction("This function expect a value"); |
|
133 | - } |
|
134 | - |
|
135 | - $staticFindInstance = new static(); |
|
136 | - $staticFindInstance->id = $id == "" ? false : $id; |
|
137 | - |
|
138 | - return $staticFindInstance; |
|
79 | + /** |
|
80 | + * This method create or update record in a database table |
|
81 | + * @params void |
|
82 | + * @return bool true or false; |
|
83 | + * @throws EmptyArrayException |
|
84 | + * @throws NoRecordInsertionException |
|
85 | + * @throws NoRecordUpdateException |
|
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 | + |
|
102 | + throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
103 | + } |
|
104 | + |
|
105 | + throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
106 | + } |
|
107 | + |
|
108 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
109 | + |
|
110 | + if ($boolCommit) { |
|
111 | + return true; |
|
112 | + } |
|
113 | + |
|
114 | + throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * This method find a record by id |
|
119 | + * @params int id |
|
120 | + * @return Object |
|
121 | + * @throws NoArgumentPassedToFunctionException |
|
122 | + */ |
|
123 | + public static function find($id) |
|
124 | + { |
|
125 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
126 | + |
|
127 | + if ($num_args == 0 || $num_args > 1) { |
|
128 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
129 | + } |
|
130 | + |
|
131 | + if ($id == "") { |
|
132 | + throw NullArgumentPassedToFunction::checkNullArgumentPassedToFunction("This function expect a value"); |
|
133 | + } |
|
134 | + |
|
135 | + $staticFindInstance = new static(); |
|
136 | + $staticFindInstance->id = $id == "" ? false : $id; |
|
137 | + |
|
138 | + return $staticFindInstance; |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * This method delete a row from the table by the row id |
|
144 | - * @params int id |
|
145 | - * @return boolean true or false |
|
146 | - * @throws NoRecordDeletionException; |
|
147 | - */ |
|
148 | - public static function destroy($id) |
|
149 | - { |
|
150 | - $boolDeleted = false; |
|
151 | - |
|
152 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
142 | + /** |
|
143 | + * This method delete a row from the table by the row id |
|
144 | + * @params int id |
|
145 | + * @return boolean true or false |
|
146 | + * @throws NoRecordDeletionException; |
|
147 | + */ |
|
148 | + public static function destroy($id) |
|
149 | + { |
|
150 | + $boolDeleted = false; |
|
151 | + |
|
152 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
153 | 153 | |
154 | - if ($num_args == 0 || $num_args > 1) { |
|
155 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
156 | - } |
|
154 | + if ($num_args == 0 || $num_args > 1) { |
|
155 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
156 | + } |
|
157 | 157 | |
158 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
158 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
159 | 159 | |
160 | - if ($boolDeleted) { |
|
161 | - return true; |
|
160 | + if ($boolDeleted) { |
|
161 | + return true; |
|
162 | 162 | |
163 | - } |
|
163 | + } |
|
164 | 164 | |
165 | - throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
166 | - } |
|
165 | + throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
166 | + } |
|
167 | 167 | |
168 | 168 | /** |
169 | 169 | * This method return the current class name |
@@ -172,11 +172,11 @@ discard block |
||
172 | 172 | */ |
173 | 173 | public static function getClassName() |
174 | 174 | { |
175 | - $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
175 | + $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
176 | 176 | |
177 | - $className = end($tableName); |
|
177 | + $className = end($tableName); |
|
178 | 178 | |
179 | - return self::pluralize(strtolower($className)); |
|
179 | + return self::pluralize(strtolower($className)); |
|
180 | 180 | |
181 | 181 | } |
182 | 182 | |
@@ -185,14 +185,14 @@ discard block |
||
185 | 185 | * @param $arrayOfRecord |
186 | 186 | * @return bool |
187 | 187 | */ |
188 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
189 | - { |
|
190 | - if (count($arrayOfRecord) > 0) { |
|
191 | - return true; |
|
188 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
189 | + { |
|
190 | + if (count($arrayOfRecord) > 0) { |
|
191 | + return true; |
|
192 | 192 | |
193 | - } |
|
193 | + } |
|
194 | 194 | |
195 | - return false; |
|
196 | - } |
|
195 | + return false; |
|
196 | + } |
|
197 | 197 | |
198 | 198 | } |
@@ -14,75 +14,75 @@ |
||
14 | 14 | |
15 | 15 | class DatabaseConnection extends PDO { |
16 | 16 | |
17 | - private $databaseName; |
|
18 | - private $databaseHost; |
|
19 | - private $databaseDriver; |
|
20 | - private $databaseUsername; |
|
21 | - private $databasePassword; |
|
17 | + private $databaseName; |
|
18 | + private $databaseHost; |
|
19 | + private $databaseDriver; |
|
20 | + private $databaseUsername; |
|
21 | + private $databasePassword; |
|
22 | 22 | |
23 | - public function __construct() |
|
24 | - { |
|
25 | - $this->loadEnv(); // load the environment variables |
|
23 | + public function __construct() |
|
24 | + { |
|
25 | + $this->loadEnv(); // load the environment variables |
|
26 | 26 | |
27 | - $this->databaseName = getenv('databaseName'); |
|
28 | - $this->databaseHost = getenv('databaseHost'); |
|
29 | - $this->databaseDriver = getenv('databaseDriver'); |
|
30 | - $this->databaseUsername = getenv('databaseUsername'); |
|
31 | - $this->databasePassword = getenv('databasePassword'); |
|
27 | + $this->databaseName = getenv('databaseName'); |
|
28 | + $this->databaseHost = getenv('databaseHost'); |
|
29 | + $this->databaseDriver = getenv('databaseDriver'); |
|
30 | + $this->databaseUsername = getenv('databaseUsername'); |
|
31 | + $this->databasePassword = getenv('databasePassword'); |
|
32 | 32 | |
33 | - try { |
|
34 | - $options = [ |
|
35 | - PDO::ATTR_PERSISTENT => true, |
|
36 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
|
37 | - ]; |
|
33 | + try { |
|
34 | + $options = [ |
|
35 | + PDO::ATTR_PERSISTENT => true, |
|
36 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
|
37 | + ]; |
|
38 | 38 | |
39 | - parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
39 | + parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options); |
|
40 | 40 | |
41 | - } catch(PDOException $e) { |
|
42 | - return $e->getMessage(); |
|
41 | + } catch(PDOException $e) { |
|
42 | + return $e->getMessage(); |
|
43 | 43 | |
44 | - } |
|
44 | + } |
|
45 | 45 | |
46 | - } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * This method determines the driver to be used for appropriate database server |
|
50 | - * @params void |
|
51 | - * @return string dsn |
|
52 | - */ |
|
53 | - public function getDatabaseDriver() |
|
54 | - { |
|
55 | - $dsn = ""; |
|
48 | + /** |
|
49 | + * This method determines the driver to be used for appropriate database server |
|
50 | + * @params void |
|
51 | + * @return string dsn |
|
52 | + */ |
|
53 | + public function getDatabaseDriver() |
|
54 | + { |
|
55 | + $dsn = ""; |
|
56 | 56 | |
57 | - switch ($this->databaseDriver) { |
|
58 | - case 'mysql': |
|
59 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN |
|
60 | - break; |
|
61 | - case 'sqlite': |
|
62 | - $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
63 | - break; |
|
64 | - case 'pgsql': |
|
65 | - $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
66 | - break; |
|
67 | - default: |
|
68 | - $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
69 | - break; |
|
70 | - } |
|
57 | + switch ($this->databaseDriver) { |
|
58 | + case 'mysql': |
|
59 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN |
|
60 | + break; |
|
61 | + case 'sqlite': |
|
62 | + $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
63 | + break; |
|
64 | + case 'pgsql': |
|
65 | + $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
66 | + break; |
|
67 | + default: |
|
68 | + $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; |
|
69 | + break; |
|
70 | + } |
|
71 | 71 | |
72 | - return $dsn; |
|
72 | + return $dsn; |
|
73 | 73 | |
74 | - } |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Load Dotenv to grant getenv() access to environment variables in .env file |
|
78 | - */ |
|
79 | - public function loadEnv() |
|
80 | - { |
|
81 | - if (! getenv("APP_ENV")) { |
|
82 | - $dotenv = new Dotenv(__DIR__.'/../../'); |
|
83 | - $dotenv->load(); |
|
84 | - } |
|
76 | + /** |
|
77 | + * Load Dotenv to grant getenv() access to environment variables in .env file |
|
78 | + */ |
|
79 | + public function loadEnv() |
|
80 | + { |
|
81 | + if (! getenv("APP_ENV")) { |
|
82 | + $dotenv = new Dotenv(__DIR__.'/../../'); |
|
83 | + $dotenv->load(); |
|
84 | + } |
|
85 | 85 | |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | 88 | } |
@@ -12,34 +12,34 @@ |
||
12 | 12 | |
13 | 13 | class DatabaseHelper { |
14 | 14 | |
15 | - public $dbConn; |
|
15 | + public $dbConn; |
|
16 | 16 | |
17 | - /** |
|
18 | - * This is a constructor; a default method that will be called automatically during class instantiation |
|
19 | - */ |
|
20 | - public function __construct($dbConnect) |
|
21 | - { |
|
22 | - $this->dbConn = $dbConnect; |
|
23 | - |
|
24 | - } |
|
17 | + /** |
|
18 | + * This is a constructor; a default method that will be called automatically during class instantiation |
|
19 | + */ |
|
20 | + public function __construct($dbConnect) |
|
21 | + { |
|
22 | + $this->dbConn = $dbConnect; |
|
23 | + |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * This method creates a particular table |
|
28 | - * @param tableName |
|
29 | - * $return boolean true or false |
|
30 | - */ |
|
31 | - public function createTable($tableName, $conn = NULL) |
|
32 | - { |
|
33 | - if (is_null($conn)) { |
|
34 | - $conn = $this->dbConn; |
|
35 | - } |
|
36 | - |
|
37 | - $sql = 'CREATE TABLE IF NOT EXISTS '.$tableName.'('; |
|
38 | - $sql.= ' id INT( 11 ) AUTO_INCREMENT PRIMARY KEY, name VARCHAR( 100 ), gender VARCHAR( 10 ), alias VARCHAR( 150 ) NOT NULL, class VARCHAR( 150 ), stack VARCHAR( 50 ) )'; |
|
39 | - |
|
40 | - return $conn->exec($sql); |
|
41 | - |
|
42 | - throw TableNotCreatedException::checkTableNotCreatedException("Check your database connection"); |
|
26 | + /** |
|
27 | + * This method creates a particular table |
|
28 | + * @param tableName |
|
29 | + * $return boolean true or false |
|
30 | + */ |
|
31 | + public function createTable($tableName, $conn = NULL) |
|
32 | + { |
|
33 | + if (is_null($conn)) { |
|
34 | + $conn = $this->dbConn; |
|
35 | + } |
|
36 | + |
|
37 | + $sql = 'CREATE TABLE IF NOT EXISTS '.$tableName.'('; |
|
38 | + $sql.= ' id INT( 11 ) AUTO_INCREMENT PRIMARY KEY, name VARCHAR( 100 ), gender VARCHAR( 10 ), alias VARCHAR( 150 ) NOT NULL, class VARCHAR( 150 ), stack VARCHAR( 50 ) )'; |
|
39 | + |
|
40 | + return $conn->exec($sql); |
|
41 | + |
|
42 | + throw TableNotCreatedException::checkTableNotCreatedException("Check your database connection"); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | } |
@@ -12,10 +12,10 @@ |
||
12 | 12 | |
13 | 13 | class NullArgumentPassedToFunction extends Exception { |
14 | 14 | |
15 | - public static function checkNullArgumentPassedToFunction($message) |
|
16 | - { |
|
17 | - return new static ($message); |
|
15 | + public static function checkNullArgumentPassedToFunction($message) |
|
16 | + { |
|
17 | + return new static ($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | |
21 | 21 | } |
@@ -12,10 +12,10 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordFoundException extends Exception { |
14 | 14 | |
15 | - public static function checkNoRecordFoundException($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function checkNoRecordFoundException($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | |
21 | 21 | } |
@@ -12,10 +12,10 @@ |
||
12 | 12 | |
13 | 13 | class TableNotCreatedException extends Exception { |
14 | 14 | |
15 | - public static function checkTableNotCreatedException($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function checkTableNotCreatedException($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | |
21 | 21 | } |
@@ -12,9 +12,9 @@ |
||
12 | 12 | |
13 | 13 | class NoRecordDeletionException extends Exception |
14 | 14 | { |
15 | - public static function checkNoRecordDeleteException($message) |
|
16 | - { |
|
17 | - return new static($message); |
|
15 | + public static function checkNoRecordDeleteException($message) |
|
16 | + { |
|
17 | + return new static($message); |
|
18 | 18 | |
19 | - } |
|
19 | + } |
|
20 | 20 | } |