Upload beautify
This commit is contained in:
37
app/Uploads/Services/SlugService.php
Normal file
37
app/Uploads/Services/SlugService.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Uploads\Services;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
final class SlugService
|
||||
{
|
||||
public function makeSlug(string $title): string
|
||||
{
|
||||
$base = Str::slug($title);
|
||||
if ($base === '') {
|
||||
$base = 'artwork';
|
||||
}
|
||||
|
||||
$candidate = $base;
|
||||
$suffix = 2;
|
||||
|
||||
while ($this->publishedSlugExists($candidate)) {
|
||||
$candidate = $base . '-' . $suffix;
|
||||
$suffix++;
|
||||
}
|
||||
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
private function publishedSlugExists(string $slug): bool
|
||||
{
|
||||
return DB::table('uploads')
|
||||
->where('status', 'published')
|
||||
->where('slug', $slug)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user