Skip to content

Commit 0ab929b

Browse files
committed
🔧 modify use_table() helper function to use the match syntax
Signed-off-by: otengkwame <developerkwame@gmail.com>
1 parent a34e71f commit 0ab929b

1 file changed

Lines changed: 16 additions & 26 deletions

File tree

Core/helpers/db_helper.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,24 @@
2626
* @param string $table
2727
* @return object
2828
*/
29-
function use_table($table = '', $model_type = 'EasyModel')
30-
{
31-
if ($model_type === 'EasyModel') {
32-
$model = new \Base\Models\EasyModel;
33-
}
34-
35-
if ($model_type === 'BaseModel') {
36-
$model = new \Base\Models\BaseModel();
37-
}
38-
39-
if ($model_type === 'OrmModel') {
40-
$model = new \Base\Models\OrmModel;
41-
}
42-
43-
if ($model_type === 'Model') {
44-
$model = new \Base\Models\Model;
45-
}
46-
29+
function use_table($table = '', $with = 'EasyModel')
30+
{
4731
// This will default to EasyModel unless
48-
// $model_type is changed appropriately
49-
if (empty($table)) {
50-
return $model;
51-
}
52-
53-
$model->{'table'} = $table; // bypass dynamic property error
32+
// Model type $with is changed appropriately
33+
$model = match ($with) {
34+
'EasyModel' => new \Base\Models\EasyModel,
35+
'BaseModel' => new \Base\Models\BaseModel,
36+
'OrmModel' => new \Base\Models\OrmModel,
37+
'Model' => new \Base\Models\Model,
38+
default => new \Base\Models\EasyModel, // Default to EasyModel
39+
};
40+
41+
if (!empty($table)) {
42+
$model->{'table'} = $table; // bypass dynamic property error
43+
}
5444

55-
return $model;
56-
}
45+
return $model;
46+
}
5747
}
5848

5949
if ( ! function_exists( 'use_db' ))

0 commit comments

Comments
 (0)