@@ -8,39 +8,39 @@ |
||
8 | 8 | |
9 | 9 | interface InterfaceBaseClass |
10 | 10 | { |
11 | - /** |
|
12 | - * This method gets all the record from a particular table. |
|
13 | - * |
|
14 | - * @params void |
|
15 | - * |
|
16 | - * @return associative array |
|
17 | - */ |
|
18 | - public static function getAll(); |
|
11 | + /** |
|
12 | + * This method gets all the record from a particular table. |
|
13 | + * |
|
14 | + * @params void |
|
15 | + * |
|
16 | + * @return associative array |
|
17 | + */ |
|
18 | + public static function getAll(); |
|
19 | 19 | |
20 | - /** |
|
21 | - * This method create or update record in a database table. |
|
22 | - * |
|
23 | - * @params void |
|
24 | - * |
|
25 | - * @return bool true or false; |
|
26 | - */ |
|
27 | - public function save(); |
|
20 | + /** |
|
21 | + * This method create or update record in a database table. |
|
22 | + * |
|
23 | + * @params void |
|
24 | + * |
|
25 | + * @return bool true or false; |
|
26 | + */ |
|
27 | + public function save(); |
|
28 | 28 | |
29 | - /** |
|
30 | - * This method delete a row from the table by the row id. |
|
31 | - * |
|
32 | - * @params int $id |
|
33 | - * |
|
34 | - * @return bool true or false |
|
35 | - */ |
|
36 | - public static function destroy($id); |
|
29 | + /** |
|
30 | + * This method delete a row from the table by the row id. |
|
31 | + * |
|
32 | + * @params int $id |
|
33 | + * |
|
34 | + * @return bool true or false |
|
35 | + */ |
|
36 | + public static function destroy($id); |
|
37 | 37 | |
38 | - /** |
|
39 | - * This method find a record by id. |
|
40 | - * |
|
41 | - * @params int $id |
|
42 | - * |
|
43 | - * @return object find |
|
44 | - */ |
|
45 | - public static function find($id); |
|
38 | + /** |
|
39 | + * This method find a record by id. |
|
40 | + * |
|
41 | + * @params int $id |
|
42 | + * |
|
43 | + * @return object find |
|
44 | + */ |
|
45 | + public static function find($id); |
|
46 | 46 | } |
@@ -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 | } |
@@ -8,32 +8,32 @@ |
||
8 | 8 | |
9 | 9 | class DatabaseHelper |
10 | 10 | { |
11 | - public $dbConn; |
|
11 | + public $dbConn; |
|
12 | 12 | |
13 | - /** |
|
14 | - * This is a constructor; a default method that will be called automatically during class instantiation. |
|
15 | - */ |
|
16 | - public function __construct($dbConnect) |
|
17 | - { |
|
18 | - $this->dbConn = $dbConnect; |
|
19 | - } |
|
13 | + /** |
|
14 | + * This is a constructor; a default method that will be called automatically during class instantiation. |
|
15 | + */ |
|
16 | + public function __construct($dbConnect) |
|
17 | + { |
|
18 | + $this->dbConn = $dbConnect; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * This method creates a particular table. |
|
23 | - * |
|
24 | - * @param tableName |
|
25 | - * $return boolean true or false |
|
26 | - */ |
|
27 | - public function createTable($tableName, $conn = null) |
|
28 | - { |
|
29 | - if (is_null($conn)) { |
|
30 | - $conn = $this->dbConn; |
|
31 | - } |
|
21 | + /** |
|
22 | + * This method creates a particular table. |
|
23 | + * |
|
24 | + * @param tableName |
|
25 | + * $return boolean true or false |
|
26 | + */ |
|
27 | + public function createTable($tableName, $conn = null) |
|
28 | + { |
|
29 | + if (is_null($conn)) { |
|
30 | + $conn = $this->dbConn; |
|
31 | + } |
|
32 | 32 | |
33 | - $sql = 'CREATE TABLE IF NOT EXISTS '.$tableName.'('; |
|
34 | - $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 ) )'; |
|
33 | + $sql = 'CREATE TABLE IF NOT EXISTS '.$tableName.'('; |
|
34 | + $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 ) )'; |
|
35 | 35 | |
36 | - return $conn->exec($sql); |
|
36 | + return $conn->exec($sql); |
|
37 | 37 | |
38 | - } |
|
38 | + } |
|
39 | 39 | } |
@@ -8,169 +8,169 @@ 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($dbConn = Null) |
|
88 | - { |
|
89 | - if (is_null($dbConn)) { |
|
90 | - $dbConn = new DatabaseConnection(); |
|
91 | - } |
|
92 | - |
|
93 | - $boolCommit = false; |
|
94 | - |
|
95 | - if ($this->properties['id']) { |
|
96 | - |
|
97 | - $allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn); |
|
98 | - |
|
99 | - if ($this->checkIfRecordIsEmpty($allData)) { |
|
100 | - $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn); |
|
101 | - |
|
102 | - if ($boolCommit) { |
|
103 | - return true; |
|
104 | - } |
|
105 | - |
|
106 | - throw NoRecordUpdateException::create('Record not updated successfully'); |
|
107 | - } |
|
108 | - |
|
109 | - throw EmptyArrayException::create("Value passed didn't match any record"); |
|
110 | - } |
|
111 | - |
|
112 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn); |
|
113 | - |
|
114 | - if ($boolCommit) { |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - throw NoRecordInsertionException::create('Record not created successfully'); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * This method find a record by id. |
|
123 | - * |
|
124 | - * @params int id |
|
125 | - * |
|
126 | - * @throws NoArgumentPassedToFunctionException |
|
127 | - * |
|
128 | - * @return object |
|
129 | - */ |
|
130 | - public static function find($id) |
|
131 | - { |
|
132 | - $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
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 NullArgumentPassedToFunctionException::create('This function expect a value'); |
|
139 | - } |
|
140 | - |
|
141 | - $staticFindInstance = new static(); |
|
142 | - $staticFindInstance->id = $id == '' ? false : $id; |
|
143 | - |
|
144 | - return $staticFindInstance; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * This method delete a row from the table by the row id. |
|
149 | - * |
|
150 | - * @params int id |
|
151 | - * |
|
152 | - * @throws NoRecordDeletionException; |
|
153 | - * |
|
154 | - * @return bool true or false |
|
155 | - */ |
|
156 | - public static function destroy($id) |
|
157 | - { |
|
158 | - $boolDeleted = false; |
|
159 | - |
|
160 | - $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
161 | - |
|
162 | - if ($num_args == 0 || $num_args > 1) { |
|
163 | - throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
164 | - } |
|
165 | - |
|
166 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
167 | - |
|
168 | - if ($boolDeleted) { |
|
169 | - return true; |
|
170 | - } |
|
171 | - |
|
172 | - throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
173 | - } |
|
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($dbConn = Null) |
|
88 | + { |
|
89 | + if (is_null($dbConn)) { |
|
90 | + $dbConn = new DatabaseConnection(); |
|
91 | + } |
|
92 | + |
|
93 | + $boolCommit = false; |
|
94 | + |
|
95 | + if ($this->properties['id']) { |
|
96 | + |
|
97 | + $allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn); |
|
98 | + |
|
99 | + if ($this->checkIfRecordIsEmpty($allData)) { |
|
100 | + $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn); |
|
101 | + |
|
102 | + if ($boolCommit) { |
|
103 | + return true; |
|
104 | + } |
|
105 | + |
|
106 | + throw NoRecordUpdateException::create('Record not updated successfully'); |
|
107 | + } |
|
108 | + |
|
109 | + throw EmptyArrayException::create("Value passed didn't match any record"); |
|
110 | + } |
|
111 | + |
|
112 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn); |
|
113 | + |
|
114 | + if ($boolCommit) { |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + throw NoRecordInsertionException::create('Record not created successfully'); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * This method find a record by id. |
|
123 | + * |
|
124 | + * @params int id |
|
125 | + * |
|
126 | + * @throws NoArgumentPassedToFunctionException |
|
127 | + * |
|
128 | + * @return object |
|
129 | + */ |
|
130 | + public static function find($id) |
|
131 | + { |
|
132 | + $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
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 NullArgumentPassedToFunctionException::create('This function expect a value'); |
|
139 | + } |
|
140 | + |
|
141 | + $staticFindInstance = new static(); |
|
142 | + $staticFindInstance->id = $id == '' ? false : $id; |
|
143 | + |
|
144 | + return $staticFindInstance; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * This method delete a row from the table by the row id. |
|
149 | + * |
|
150 | + * @params int id |
|
151 | + * |
|
152 | + * @throws NoRecordDeletionException; |
|
153 | + * |
|
154 | + * @return bool true or false |
|
155 | + */ |
|
156 | + public static function destroy($id) |
|
157 | + { |
|
158 | + $boolDeleted = false; |
|
159 | + |
|
160 | + $num_args = (int) func_num_args(); // get number of arguments passed to this function |
|
161 | + |
|
162 | + if ($num_args == 0 || $num_args > 1) { |
|
163 | + throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
|
164 | + } |
|
165 | + |
|
166 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
167 | + |
|
168 | + if ($boolDeleted) { |
|
169 | + return true; |
|
170 | + } |
|
171 | + |
|
172 | + throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
173 | + } |
|
174 | 174 | |
175 | 175 | /** |
176 | 176 | * This method return the current class name |
@@ -180,26 +180,26 @@ discard block |
||
180 | 180 | */ |
181 | 181 | public static function getClassName() |
182 | 182 | { |
183 | - $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
183 | + $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
|
184 | 184 | |
185 | - $className = end($tableName); |
|
185 | + $className = end($tableName); |
|
186 | 186 | |
187 | - return self::pluralize(strtolower($className)); |
|
187 | + return self::pluralize(strtolower($className)); |
|
188 | 188 | } |
189 | 189 | |
190 | - /** |
|
191 | - * This method check if the argument passed to this function is an array. |
|
192 | - * |
|
193 | - * @param $arrayOfRecord |
|
194 | - * |
|
195 | - * @return bool |
|
196 | - */ |
|
197 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
198 | - { |
|
199 | - if (count($arrayOfRecord) > 0) { |
|
200 | - return true; |
|
201 | - } |
|
202 | - |
|
203 | - return false; |
|
204 | - } |
|
190 | + /** |
|
191 | + * This method check if the argument passed to this function is an array. |
|
192 | + * |
|
193 | + * @param $arrayOfRecord |
|
194 | + * |
|
195 | + * @return bool |
|
196 | + */ |
|
197 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
198 | + { |
|
199 | + if (count($arrayOfRecord) > 0) { |
|
200 | + return true; |
|
201 | + } |
|
202 | + |
|
203 | + return false; |
|
204 | + } |
|
205 | 205 | } |
@@ -10,128 +10,128 @@ discard block |
||
10 | 10 | |
11 | 11 | class DatabaseHandler |
12 | 12 | { |
13 | - private $tableFields; |
|
14 | - private $dbHelperInstance; |
|
15 | - private $dbConnection; |
|
16 | - private $model; |
|
17 | - |
|
18 | - /** |
|
19 | - * This is a constructor; a default method that will be called automatically during class instantiation. |
|
20 | - */ |
|
21 | - public function __construct($modelClassName, $dbConn = null) |
|
22 | - { |
|
23 | - if (is_null($dbConn)) { |
|
24 | - $this->dbConnection = new DatabaseConnection(); |
|
25 | - } else { |
|
26 | - $this->dbConnection = $dbConn; |
|
27 | - } |
|
28 | - |
|
29 | - $this->model = $modelClassName; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * This method create a record and store it in a table row. |
|
34 | - * |
|
35 | - * @params associative array, string tablename |
|
36 | - * |
|
37 | - * @return bool true or false |
|
38 | - */ |
|
39 | - public function create($associative1DArray, $tableName, $dbConn = Null) |
|
40 | - { |
|
41 | - if (is_null($dbConn)) { |
|
42 | - $dbConn = $this->dbConnection; |
|
43 | - } |
|
44 | - |
|
45 | - $tableFields = $this->getColumnNames($this->model, $dbConn); |
|
46 | - |
|
47 | - $unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray); |
|
48 | - |
|
49 | - if (count($unexpectedFields) > 0) { |
|
50 | - throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
51 | - } |
|
52 | - |
|
53 | - unset($associative1DArray[0]); |
|
54 | - |
|
55 | - return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
13 | + private $tableFields; |
|
14 | + private $dbHelperInstance; |
|
15 | + private $dbConnection; |
|
16 | + private $model; |
|
17 | + |
|
18 | + /** |
|
19 | + * This is a constructor; a default method that will be called automatically during class instantiation. |
|
20 | + */ |
|
21 | + public function __construct($modelClassName, $dbConn = null) |
|
22 | + { |
|
23 | + if (is_null($dbConn)) { |
|
24 | + $this->dbConnection = new DatabaseConnection(); |
|
25 | + } else { |
|
26 | + $this->dbConnection = $dbConn; |
|
27 | + } |
|
28 | + |
|
29 | + $this->model = $modelClassName; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * This method create a record and store it in a table row. |
|
34 | + * |
|
35 | + * @params associative array, string tablename |
|
36 | + * |
|
37 | + * @return bool true or false |
|
38 | + */ |
|
39 | + public function create($associative1DArray, $tableName, $dbConn = Null) |
|
40 | + { |
|
41 | + if (is_null($dbConn)) { |
|
42 | + $dbConn = $this->dbConnection; |
|
43 | + } |
|
44 | + |
|
45 | + $tableFields = $this->getColumnNames($this->model, $dbConn); |
|
46 | + |
|
47 | + $unexpectedFields = self::filterClassAttributes($tableFields, $associative1DArray); |
|
48 | + |
|
49 | + if (count($unexpectedFields) > 0) { |
|
50 | + throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
51 | + } |
|
52 | + |
|
53 | + unset($associative1DArray[0]); |
|
54 | + |
|
55 | + return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
56 | 56 | |
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * This method runs the insertion query. |
|
62 | - * |
|
63 | - * @param $dbConn |
|
64 | - * @param $tableName |
|
65 | - * @param $associative1DArray |
|
66 | - * |
|
67 | - * @return bool true |
|
68 | - */ |
|
69 | - private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
70 | - { |
|
71 | - $insertQuery = 'INSERT INTO '.$tableName; |
|
60 | + /** |
|
61 | + * This method runs the insertion query. |
|
62 | + * |
|
63 | + * @param $dbConn |
|
64 | + * @param $tableName |
|
65 | + * @param $associative1DArray |
|
66 | + * |
|
67 | + * @return bool true |
|
68 | + */ |
|
69 | + private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
70 | + { |
|
71 | + $insertQuery = 'INSERT INTO '.$tableName; |
|
72 | 72 | |
73 | - $TableValues = implode(',', array_keys($associative1DArray)); |
|
73 | + $TableValues = implode(',', array_keys($associative1DArray)); |
|
74 | 74 | |
75 | - foreach ($associative1DArray as $field => $value) { |
|
76 | - $FormValues[] = "'".trim(addslashes($value))."'"; |
|
77 | - } |
|
75 | + foreach ($associative1DArray as $field => $value) { |
|
76 | + $FormValues[] = "'".trim(addslashes($value))."'"; |
|
77 | + } |
|
78 | 78 | |
79 | - $splittedTableValues = implode(',', $FormValues); |
|
79 | + $splittedTableValues = implode(',', $FormValues); |
|
80 | 80 | |
81 | - $insertQuery .= ' ('.$TableValues.')'; |
|
82 | - $insertQuery .= ' VALUES ('.$splittedTableValues.')'; |
|
81 | + $insertQuery .= ' ('.$TableValues.')'; |
|
82 | + $insertQuery .= ' VALUES ('.$splittedTableValues.')'; |
|
83 | 83 | |
84 | - $executeQuery = $dbConn->exec($insertQuery); |
|
84 | + $executeQuery = $dbConn->exec($insertQuery); |
|
85 | 85 | |
86 | - return $executeQuery; |
|
86 | + return $executeQuery; |
|
87 | 87 | |
88 | - } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * This method updates any table by supplying 3 parameter. |
|
92 | - * |
|
93 | - * @params: $updateParams, $tableName, $associative1DArray |
|
94 | - * |
|
95 | - * @return bool true or false |
|
96 | - */ |
|
97 | - public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
98 | - { |
|
99 | - $sql = ''; |
|
90 | + /** |
|
91 | + * This method updates any table by supplying 3 parameter. |
|
92 | + * |
|
93 | + * @params: $updateParams, $tableName, $associative1DArray |
|
94 | + * |
|
95 | + * @return bool true or false |
|
96 | + */ |
|
97 | + public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
98 | + { |
|
99 | + $sql = ''; |
|
100 | 100 | |
101 | - if (is_null($dbConn)) { |
|
102 | - $dbConn = $this->dbConnection; |
|
103 | - } |
|
101 | + if (is_null($dbConn)) { |
|
102 | + $dbConn = $this->dbConnection; |
|
103 | + } |
|
104 | 104 | |
105 | - $updateSql = "UPDATE `$tableName` SET "; |
|
105 | + $updateSql = "UPDATE `$tableName` SET "; |
|
106 | 106 | |
107 | - unset($associative1DArray['id']); |
|
107 | + unset($associative1DArray['id']); |
|
108 | 108 | |
109 | - $unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $dbConn), $associative1DArray); |
|
109 | + $unexpectedFields = self::filterClassAttributes($this->getColumnNames($this->model, $dbConn), $associative1DArray); |
|
110 | 110 | |
111 | - if (count($unexpectedFields) > 0) { |
|
112 | - throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
113 | - } |
|
111 | + if (count($unexpectedFields) > 0) { |
|
112 | + throw TableFieldUndefinedException::create($unexpectedFields, 'needs to be created as a table field'); |
|
113 | + } |
|
114 | 114 | |
115 | - foreach ($associative1DArray as $field => $value) { |
|
116 | - $sql .= "`$field` = '$value'".','; |
|
117 | - } |
|
115 | + foreach ($associative1DArray as $field => $value) { |
|
116 | + $sql .= "`$field` = '$value'".','; |
|
117 | + } |
|
118 | 118 | |
119 | - $updateSql .= $this->prepareUpdateQuery($sql); |
|
119 | + $updateSql .= $this->prepareUpdateQuery($sql); |
|
120 | 120 | |
121 | - foreach ($updateParams as $key => $val) { |
|
122 | - $updateSql .= " WHERE $key = $val"; |
|
123 | - } |
|
121 | + foreach ($updateParams as $key => $val) { |
|
122 | + $updateSql .= " WHERE $key = $val"; |
|
123 | + } |
|
124 | 124 | |
125 | - $stmt = $dbConn->prepare($updateSql); |
|
125 | + $stmt = $dbConn->prepare($updateSql); |
|
126 | 126 | |
127 | - $boolResponse = $stmt->execute(); |
|
127 | + $boolResponse = $stmt->execute(); |
|
128 | 128 | |
129 | - if ($boolResponse) { |
|
130 | - return true; |
|
131 | - } |
|
129 | + if ($boolResponse) { |
|
130 | + return true; |
|
131 | + } |
|
132 | 132 | |
133 | - return false; |
|
134 | - } |
|
133 | + return false; |
|
134 | + } |
|
135 | 135 | |
136 | 136 | /** |
137 | 137 | * This method retrieves record from a table. |
@@ -142,26 +142,26 @@ discard block |
||
142 | 142 | */ |
143 | 143 | public static function read($id, $tableName, $dbConn = null) |
144 | 144 | { |
145 | - $tableData = []; |
|
145 | + $tableData = []; |
|
146 | 146 | |
147 | - if (is_null($dbConn)) { |
|
148 | - $dbConn = new DatabaseConnection(); |
|
149 | - } |
|
147 | + if (is_null($dbConn)) { |
|
148 | + $dbConn = new DatabaseConnection(); |
|
149 | + } |
|
150 | 150 | |
151 | - $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
151 | + $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
152 | 152 | |
153 | - $stmt = $dbConn->prepare($sql); |
|
154 | - $stmt->bindValue(':table', $tableName); |
|
155 | - $stmt->bindValue(':id', $id); |
|
156 | - $stmt->execute(); |
|
153 | + $stmt = $dbConn->prepare($sql); |
|
154 | + $stmt->bindValue(':table', $tableName); |
|
155 | + $stmt->bindValue(':id', $id); |
|
156 | + $stmt->execute(); |
|
157 | 157 | |
158 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
158 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
159 | 159 | |
160 | - foreach ($results as $result) { |
|
161 | - array_push($tableData, $result); |
|
162 | - } |
|
160 | + foreach ($results as $result) { |
|
161 | + array_push($tableData, $result); |
|
162 | + } |
|
163 | 163 | |
164 | - return $tableData; |
|
164 | + return $tableData; |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -173,19 +173,19 @@ discard block |
||
173 | 173 | */ |
174 | 174 | public static function delete($id, $tableName, $dbConn = null) |
175 | 175 | { |
176 | - if (is_null($dbConn)) { |
|
177 | - $dbConn = new DatabaseConnection(); |
|
178 | - } |
|
176 | + if (is_null($dbConn)) { |
|
177 | + $dbConn = new DatabaseConnection(); |
|
178 | + } |
|
179 | 179 | |
180 | - $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
180 | + $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
181 | 181 | |
182 | - $boolResponse = $dbConn->exec($sql); |
|
182 | + $boolResponse = $dbConn->exec($sql); |
|
183 | 183 | |
184 | - if ($boolResponse) { |
|
185 | - return true; |
|
186 | - } |
|
184 | + if ($boolResponse) { |
|
185 | + return true; |
|
186 | + } |
|
187 | 187 | |
188 | - throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
188 | + throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
@@ -198,15 +198,15 @@ discard block |
||
198 | 198 | */ |
199 | 199 | public static function filterClassAttributes(array $tableColumn, array $userSetterArray) |
200 | 200 | { |
201 | - $unexpectedFields = []; |
|
201 | + $unexpectedFields = []; |
|
202 | 202 | |
203 | - foreach ($userSetterArray as $key => $val) { |
|
204 | - if (! in_array($key, $tableColumn)) { |
|
205 | - $unexpectedFields[] = $key; |
|
206 | - } |
|
207 | - } |
|
203 | + foreach ($userSetterArray as $key => $val) { |
|
204 | + if (! in_array($key, $tableColumn)) { |
|
205 | + $unexpectedFields[] = $key; |
|
206 | + } |
|
207 | + } |
|
208 | 208 | |
209 | - return $unexpectedFields; |
|
209 | + return $unexpectedFields; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -218,13 +218,13 @@ discard block |
||
218 | 218 | */ |
219 | 219 | public function prepareUpdateQuery($sql) |
220 | 220 | { |
221 | - $splittedQuery = explode(',', $sql); |
|
221 | + $splittedQuery = explode(',', $sql); |
|
222 | 222 | |
223 | - array_pop($splittedQuery); |
|
223 | + array_pop($splittedQuery); |
|
224 | 224 | |
225 | - $mergeData = implode(',', $splittedQuery); |
|
225 | + $mergeData = implode(',', $splittedQuery); |
|
226 | 226 | |
227 | - return $mergeData; |
|
227 | + return $mergeData; |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -238,26 +238,26 @@ discard block |
||
238 | 238 | */ |
239 | 239 | public function findAndWhere($params, $tableName, $dbConn = null) |
240 | 240 | { |
241 | - if (is_null($dbConn)) { |
|
242 | - $dbConn = $this->dbConnection; |
|
243 | - } |
|
241 | + if (is_null($dbConn)) { |
|
242 | + $dbConn = $this->dbConnection; |
|
243 | + } |
|
244 | 244 | |
245 | - if (is_array($params) && !empty($params)) { |
|
246 | - $sql = 'SELECT * FROM '.$tableName; |
|
245 | + if (is_array($params) && !empty($params)) { |
|
246 | + $sql = 'SELECT * FROM '.$tableName; |
|
247 | 247 | |
248 | - foreach ($params as $key => $val) { |
|
249 | - $sql .= " WHERE `$key` = '$val'"; |
|
250 | - } |
|
248 | + foreach ($params as $key => $val) { |
|
249 | + $sql .= " WHERE `$key` = '$val'"; |
|
250 | + } |
|
251 | 251 | |
252 | - $statement = $dbConn->prepare($sql); |
|
253 | - $statement->execute(); |
|
252 | + $statement = $dbConn->prepare($sql); |
|
253 | + $statement->execute(); |
|
254 | 254 | |
255 | - $returnedRowNumbers = $statement->rowCount(); |
|
255 | + $returnedRowNumbers = $statement->rowCount(); |
|
256 | 256 | |
257 | - return $returnedRowNumbers ? true : false; |
|
258 | - } |
|
257 | + return $returnedRowNumbers ? true : false; |
|
258 | + } |
|
259 | 259 | |
260 | - throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array'); |
|
260 | + throw EmptyArrayException::create('Array Expected: parameter passed to this function is not an array'); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | /** |
@@ -270,24 +270,24 @@ discard block |
||
270 | 270 | */ |
271 | 271 | public function getColumnNames($table, $dbConn = null) |
272 | 272 | { |
273 | - $tableFields = []; |
|
273 | + $tableFields = []; |
|
274 | 274 | |
275 | - if (is_null($dbConn)) { |
|
276 | - $dbConn = $this->dbConnection; |
|
277 | - } |
|
275 | + if (is_null($dbConn)) { |
|
276 | + $dbConn = $this->dbConnection; |
|
277 | + } |
|
278 | 278 | |
279 | - $sql = 'SHOW COLUMNS FROM '.$table; |
|
279 | + $sql = 'SHOW COLUMNS FROM '.$table; |
|
280 | 280 | |
281 | - $stmt = $dbConn->prepare($sql); |
|
282 | - $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
283 | - $stmt->execute(); |
|
281 | + $stmt = $dbConn->prepare($sql); |
|
282 | + $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
283 | + $stmt->execute(); |
|
284 | 284 | |
285 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
285 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
286 | 286 | |
287 | - foreach ($results as $result) { |
|
288 | - array_push($tableFields, $result['Field']); |
|
289 | - } |
|
287 | + foreach ($results as $result) { |
|
288 | + array_push($tableFields, $result['Field']); |
|
289 | + } |
|
290 | 290 | |
291 | - return $tableFields; |
|
291 | + return $tableFields; |
|
292 | 292 | } |
293 | 293 | } |