Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModelTrait for more reusability #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 2 additions & 47 deletions src/Database/Eloquent/Model.php
Expand Up @@ -4,33 +4,19 @@

namespace GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent;

use Exception;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Ramsey\Uuid\Uuid;

abstract class Model extends BaseModel
{
use ModelTrait;

/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* Indicates if the IDs are UUIDs.
*
* @var bool
*/
protected $keyIsUuid = true;

/**
* The UUID version to use.
*
* @var int
*/
protected $uuidVersion = 4;

/**
* Indicates if the IDs are auto-incrementing.
*
Expand All @@ -55,35 +41,4 @@ abstract class Model extends BaseModel
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];

/**
* The "booting" method of the model.
*/
protected static function boot(): void
{
parent::boot();

static::creating(function (self $model): void {
// Automatically generate a UUID if using them, and not provided.
if ($model->keyIsUuid && empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = $model->generateUuid();
}
});
}

/**
* @throws \Exception
* @return string
*/
protected function generateUuid(): string
{
switch ($this->uuidVersion) {
case 1:
return Uuid::uuid1()->toString();
case 4:
return Uuid::uuid4()->toString();
}

throw new Exception("UUID version [{$this->uuidVersion}] not supported.");
}
}
54 changes: 54 additions & 0 deletions src/Database/Eloquent/ModelTrait.php
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent;

use Exception;
use Ramsey\Uuid\Uuid;

trait ModelTrait
{
/**
* Indicates if the IDs are UUIDs.
*
* @var bool
*/
protected $keyIsUuid = true;

/**
* The UUID version to use.
*
* @var int
*/
protected $uuidVersion = 4;

/**
* The "booting" method of the model.
*/
protected static function bootModelTrait(): void
{
static::creating(function (self $model): void {
// Automatically generate a UUID if using them, and not provided.
if ($model->keyIsUuid && empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = $model->generateUuid();
}
});
}

/**
* @throws \Exception
* @return string
*/
protected function generateUuid(): string
{
switch ($this->uuidVersion) {
case 1:
return Uuid::uuid1()->toString();
case 4:
return Uuid::uuid4()->toString();
}

throw new Exception("UUID version [{$this->uuidVersion}] not supported.");
}
}