60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
class EntryHelpers {
|
|
|
|
/**
|
|
* Create a unique slug.
|
|
*
|
|
* @param string $title
|
|
* @param class-string $model The model with a 'slug' field.
|
|
* @param int|null $ignoreId Ignore specific ID, like when edition
|
|
*
|
|
* @return string The original slug if no duplicates, otherwise with a number at the end.
|
|
*/
|
|
public static function uniqueSlug(string $title, string $model, ?int $ignoreId = null ): string {
|
|
|
|
$slug = Str::slug($title);
|
|
$baseSlug = $slug;
|
|
$i = 1;
|
|
while(
|
|
$model::where( 'slug', $slug )
|
|
->when($ignoreId, fn($q) => $q->where('id', '!=', $ignoreId))
|
|
->exists() && $i < 100
|
|
){
|
|
$slug = $baseSlug . '-' . $i++;
|
|
}
|
|
|
|
if( $i >= 100 ){
|
|
$slug = Str::uuid(); // Fallback...
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
/**
|
|
* Build complete title.
|
|
*
|
|
* @param string $section
|
|
* @param array $fields
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function buildCompleteTitle( string $section, array $fields = [] ){
|
|
|
|
return match ($section) {
|
|
'translations' => sprintf('%s (%s Translation) %s', $fields['entry_title'] ?? $fields['game_name'], $fields['languages_string'], $fields['platform_name']),
|
|
'romhacks' => sprintf('%s (%s) Romhack', $fields['entry_title'], $fields['platform_name']),
|
|
'homebrew' => sprintf('%s (%s) Homebrew', $fields['game_name'], $fields['platform_name']),
|
|
'utilities' => sprintf('%s - Utility', $fields['entry_title']),
|
|
'documents' => sprintf('%s - Document', $fields['entry_title']),
|
|
'lua-scripts' => sprintf('%s (%s) LUA Script', $fields['entry_title'], $fields['platform_name']),
|
|
'tutorials' => sprintf('%s - Tutorial', $fields['entry_title']),
|
|
default => $fields['entry_title'],
|
|
};
|
|
}
|
|
}
|