<?php

namespace App\Models\Concerns;

use Illuminate\Support\Str;

trait HasTurkishSlug
{
    protected function slugSourceField(): string
    {
        return 'title';
    }

    protected static function bootHasTurkishSlug(): void
    {
        static::saving(function ($model): void {
            if (empty($model->slug) && $model->{$model->slugSourceField()}) {
                $model->slug = Str::turkishSlug($model->{$model->slugSourceField()});
            } else {
                $model->slug = Str::turkishSlug($model->slug);
            }
        });
    }
}
