<?php

namespace App\Traits;

use Ramsey\Uuid\Uuid;

trait HasUuidTrait
{

    /**
     * Boot the trait.
     *
     * @return void
     */
    protected static function bootHasUuidTrait()
    {
        static::creating(function ($model) {
            if (! $model->getKey()) {
                $model->{$model->getKeyName()} = Uuid::uuid4()->toString();
            }
        });
    }

    /**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return $this->getKeyName();
    }

    /**
     * Get the primary key for the model.
     *
     * @return string
     */
    public function getKeyName()
    {
        return 'uuid';
    }

    /**
     * Get the data type of the primary key.
     *
     * @return string
     */
    public function getKeyType()
    {
        return 'string';
    }

    /**
     * Get the incrementing behavior of the primary key.
     *
     * @return bool
     */
    public function getIncrementing()
    {
        return false;
    }
}
