Skip to content

Commit

Permalink
refactor: get_class_name_by_slug function
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Feb 12, 2025
1 parent 75718ab commit b42149f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions common/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Illuminate\Database\Eloquent\Model;

if (!function_exists('get_class_name_by_slug')) {
/**
* Get class name by slug.
Expand All @@ -10,16 +12,19 @@
*/
function get_class_name_by_slug(string $slug): string
{
if ($slug === 'post') {
return \CSlant\Blog\Core\Models\Post::getBaseModel();
} elseif ($slug === 'page') {
return \CSlant\Blog\Core\Models\Page::getBaseModel();
} elseif ($slug === 'category') {
return \CSlant\Blog\Core\Models\Category::getBaseModel();
} elseif ($slug === 'tag') {
return \CSlant\Blog\Core\Models\Tag::getBaseModel();
} else {
return '';
$models = [
'post' => \CSlant\Blog\Core\Models\Post::class,
'page' => \CSlant\Blog\Core\Models\Page::class,
'category' => \CSlant\Blog\Core\Models\Category::class,
'tag' => \CSlant\Blog\Core\Models\Tag::class,
];

if (array_key_exists($slug, $models)) {
/** @var Model $models */
return $models[$slug]::getBaseModel();
}

return '';
}

}

0 comments on commit b42149f

Please sign in to comment.