Files
portal-api/app/Integrations/Whmcs/Support/SlugUniquifier.php
T
2026-09-27 11:42:40 +07:00

40 lines
1.1 KiB
PHP

<?php
namespace App\Integrations\Whmcs\Support;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SlugUniquifier
{
public function unique(string $table, string $column, string $candidate, int $legacyId): string
{
$base = Str::slug($candidate) ?: 'item';
$slug = $base;
$suffix = 1;
while ($this->ownedByAnotherRow($table, $column, $slug, $legacyId)) {
$slug = "{$base}-" . ++$suffix;
}
return $slug;
}
public function adoptableId(string $table, string $column, string $candidate): ?int
{
$slug = Str::slug($candidate) ?: 'item';
return DB::table($table)->where($column, $slug)->whereNull('legacy_id')->value('id');
}
private function ownedByAnotherRow(string $table, string $column, string $slug, int $legacyId): bool
{
return DB::table($table)
->where($column, $slug)
->where(function ($query) use ($legacyId) {
$query->whereNull('legacy_id')->orWhere('legacy_id', '!=', $legacyId);
})
->exists();
}
}