<?php

namespace App\Traits;

use App\Support\Utils;
use Hashids\HashidsException;

trait HashIdTrait
{
    /**
     * @throws HashidsException
     */
    final public static function createNewHashId(): string
    {
        return Utils::generateNewId();
    }

    /**
     * Register callbacks
     *
     * @throws HashidsException
     */
    final protected static function bootHashIdTrait(): void
    {
        static::creating(function ($model): void {
            if (empty($model->id)) {
                $model->id = static::createNewHashId();
            }
        });
    }
}
