@@ -10,5 +10,5 @@ |
||
10 | 10 | |
11 | 11 | class Inflector { |
12 | 12 | |
13 | - use Inflector; |
|
13 | + use Inflector; |
|
14 | 14 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @throws NoArgumentPassedToFunctionException |
122 | 122 | * |
123 | - * @return object |
|
123 | + * @return BaseModel |
|
124 | 124 | */ |
125 | 125 | public static function find($id) |
126 | 126 | { |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | * This method return the current class name |
172 | 172 | * $params void. |
173 | 173 | * |
174 | - * @return classname |
|
174 | + * @return string|false |
|
175 | 175 | */ |
176 | 176 | public static function getClassName() |
177 | 177 | { |
@@ -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,10 +10,10 @@ |
||
10 | 10 | |
11 | 11 | class TableFieldUndefinedException extends Exception |
12 | 12 | { |
13 | - public static function create($unExpectedFields, $message) |
|
14 | - { |
|
15 | - $fields = implode(', ', $unExpectedFields); |
|
13 | + public static function create($unExpectedFields, $message) |
|
14 | + { |
|
15 | + $fields = implode(', ', $unExpectedFields); |
|
16 | 16 | |
17 | - return new static($fields.' '.$message); |
|
18 | - } |
|
17 | + return new static($fields.' '.$message); |
|
18 | + } |
|
19 | 19 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NullArgumentPassedToFunctionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static ($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static ($message); |
|
16 | + } |
|
17 | 17 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordDeletionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static($message); |
|
16 | + } |
|
17 | 17 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordInsertionException extends Exception |
12 | 12 | { |
13 | - public static function create($mesaage) |
|
14 | - { |
|
15 | - return new static($mesaage); |
|
16 | - } |
|
13 | + public static function create($mesaage) |
|
14 | + { |
|
15 | + return new static($mesaage); |
|
16 | + } |
|
17 | 17 | } |
@@ -37,62 +37,62 @@ |
||
37 | 37 | |
38 | 38 | trait Inflector |
39 | 39 | { |
40 | - /** |
|
41 | - * Pluralizes English nouns. |
|
42 | - * |
|
43 | - * @static |
|
44 | - * |
|
45 | - * @param string $word English noun to pluralize |
|
46 | - * |
|
47 | - * @return string Plural noun |
|
48 | - */ |
|
49 | - public static function pluralize($word) |
|
50 | - { |
|
51 | - $plural = [ |
|
52 | - '/(quiz)$/i' => '$1zes', |
|
53 | - '/^(ox)$/i' => '$1en', |
|
54 | - '/([m|l])ouse$/i' => '$1ice', |
|
55 | - '/(matr|vert|ind)ix|ex$/i' => '$1ices', |
|
56 | - '/(x|ch|ss|sh)$/i' => '$1es', |
|
57 | - '/([^aeiouy]|qu)y$/i' => '$1ies', |
|
58 | - '/(hive)$/i' => '$1s', |
|
59 | - '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves', |
|
60 | - '/(shea|lea|loa|thie)f$/i' => '$1ves', |
|
61 | - '/sis$/i' => 'ses', |
|
62 | - '/([ti])um$/i' => '$1a', |
|
63 | - '/(tomat|potat|ech|her|vet)o$/i' => '$1oes', |
|
64 | - '/(bu)s$/i' => '$1ses', |
|
65 | - '/(alias)$/i' => '$1es', |
|
66 | - '/(octop)us$/i' => '$1i', |
|
67 | - '/(ax|test)is$/i' => '$1es', |
|
68 | - '/(us)$/i' => '$1es', |
|
69 | - '/s$/i' => 's', |
|
70 | - '/$/' => 's', |
|
71 | - ]; |
|
72 | - $uncountable = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep']; |
|
73 | - $irregular = [ |
|
74 | - 'person' => 'people', |
|
75 | - 'man' => 'men', |
|
76 | - 'child' => 'children', |
|
77 | - 'sex' => 'sexes', |
|
78 | - 'move' => 'moves', ]; |
|
79 | - $lowercased_word = strtolower($word); |
|
80 | - foreach ($uncountable as $_uncountable) { |
|
81 | - if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
82 | - return $word; |
|
83 | - } |
|
84 | - } |
|
85 | - foreach ($irregular as $_plural => $_singular) { |
|
86 | - if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
87 | - return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
88 | - } |
|
89 | - } |
|
90 | - foreach ($plural as $rule => $replacement) { |
|
91 | - if (preg_match($rule, $word)) { |
|
92 | - return preg_replace($rule, $replacement, $word); |
|
93 | - } |
|
94 | - } |
|
40 | + /** |
|
41 | + * Pluralizes English nouns. |
|
42 | + * |
|
43 | + * @static |
|
44 | + * |
|
45 | + * @param string $word English noun to pluralize |
|
46 | + * |
|
47 | + * @return string Plural noun |
|
48 | + */ |
|
49 | + public static function pluralize($word) |
|
50 | + { |
|
51 | + $plural = [ |
|
52 | + '/(quiz)$/i' => '$1zes', |
|
53 | + '/^(ox)$/i' => '$1en', |
|
54 | + '/([m|l])ouse$/i' => '$1ice', |
|
55 | + '/(matr|vert|ind)ix|ex$/i' => '$1ices', |
|
56 | + '/(x|ch|ss|sh)$/i' => '$1es', |
|
57 | + '/([^aeiouy]|qu)y$/i' => '$1ies', |
|
58 | + '/(hive)$/i' => '$1s', |
|
59 | + '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves', |
|
60 | + '/(shea|lea|loa|thie)f$/i' => '$1ves', |
|
61 | + '/sis$/i' => 'ses', |
|
62 | + '/([ti])um$/i' => '$1a', |
|
63 | + '/(tomat|potat|ech|her|vet)o$/i' => '$1oes', |
|
64 | + '/(bu)s$/i' => '$1ses', |
|
65 | + '/(alias)$/i' => '$1es', |
|
66 | + '/(octop)us$/i' => '$1i', |
|
67 | + '/(ax|test)is$/i' => '$1es', |
|
68 | + '/(us)$/i' => '$1es', |
|
69 | + '/s$/i' => 's', |
|
70 | + '/$/' => 's', |
|
71 | + ]; |
|
72 | + $uncountable = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep']; |
|
73 | + $irregular = [ |
|
74 | + 'person' => 'people', |
|
75 | + 'man' => 'men', |
|
76 | + 'child' => 'children', |
|
77 | + 'sex' => 'sexes', |
|
78 | + 'move' => 'moves', ]; |
|
79 | + $lowercased_word = strtolower($word); |
|
80 | + foreach ($uncountable as $_uncountable) { |
|
81 | + if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
82 | + return $word; |
|
83 | + } |
|
84 | + } |
|
85 | + foreach ($irregular as $_plural => $_singular) { |
|
86 | + if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
87 | + return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
88 | + } |
|
89 | + } |
|
90 | + foreach ($plural as $rule => $replacement) { |
|
91 | + if (preg_match($rule, $word)) { |
|
92 | + return preg_replace($rule, $replacement, $word); |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | - return false; |
|
97 | - } |
|
96 | + return false; |
|
97 | + } |
|
98 | 98 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | 'move' => 'moves', ]; |
79 | 79 | $lowercased_word = strtolower($word); |
80 | 80 | foreach ($uncountable as $_uncountable) { |
81 | - if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
81 | + if (substr($lowercased_word, (-1*strlen($_uncountable))) == $_uncountable) { |
|
82 | 82 | return $word; |
83 | 83 | } |
84 | 84 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordDeletionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static($message); |
|
16 | + } |
|
17 | 17 | } |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class NoRecordDeletionException extends Exception |
12 | 12 | { |
13 | - public static function create($message) |
|
14 | - { |
|
15 | - return new static($message); |
|
16 | - } |
|
13 | + public static function create($message) |
|
14 | + { |
|
15 | + return new static($message); |
|
16 | + } |
|
17 | 17 | } |