24 lines
646 B
PHP
24 lines
646 B
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Support;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/** Slugifies a candidate and appends -N until it's unique in the given column, excluding the row's own legacy_id. */
|
|
class SlugUniquifier
|
|
{
|
|
public function unique(string $table, string $column, string $candidate, int $legacyId): string
|
|
{
|
|
$base = Str::slug($candidate) ?: 'item';
|
|
$slug = $base;
|
|
$suffix = 1;
|
|
|
|
while (DB::table($table)->where($column, $slug)->where('legacy_id', '!=', $legacyId)->exists()) {
|
|
$slug = "{$base}-" . ++$suffix;
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
}
|