Skip to content

Commit 1a8269f

Browse files
[1.1] Feature/UUID v1 support (#6)
* Added support for UUIDv1 * Updated README * Updated composer.json * Closes #3
1 parent f467998 commit 1a8269f

File tree

11 files changed

+218
-34
lines changed

11 files changed

+218
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.idea
2+
/.php_cs.cache
23
/.phpunit.result.cache
34

45
# Created by https://www.gitignore.io/api/macos,phpstorm,composer,visualstudiocode

README.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@
3737

3838
## Introduction
3939

40-
A simple drop-in solution for providing UUIDv4 support for the IDs of your
40+
A simple drop-in solution for providing UUID support for the IDs of your
4141
Eloquent models.
4242

43+
Both `v1` and `v4` IDs are supported out of the box, however should you need
44+
`v3` or `v5` support, you can easily add this in.
45+
4346
## Installing
4447

48+
Reference the table below for the correct version to use in conjunction with the
49+
version of Laravel you have installed:
50+
51+
| Laravel | This package |
52+
| ------- | ------------ |
53+
| `v5.8.*` | `v1.*` |
54+
4555
You can install the package via composer:
4656

4757
```bash
48-
composer require goldspecdigital/laravel-eloquent-uuid
58+
composer require goldspecdigital/laravel-eloquent-uuid:~v1.1
4959
```
5060

5161
## Usage
@@ -90,8 +100,8 @@ class User extends Authenticatable
90100

91101
### Generating UUIDs
92102

93-
If you don't specify the value for the primary key of your model, a UUIDv4 will
94-
be automatically generated. However, if you do specify your own UUIDv4 then it
103+
If you don't specify the value for the primary key of your model, a UUID will
104+
be automatically generated. However, if you do specify your own UUID then it
95105
will not generate one, but instead use the one you have explicitly provided. This
96106
can be useful when needing the know the ID of the model before you have created
97107
it:
@@ -106,6 +116,56 @@ $model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
106116
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e
107117
```
108118

119+
### Specifying UUID versions
120+
121+
By default, `v4` UUIDs will be used for your models. However, you can also
122+
specify `v1` UUIDs to be used by setting the following property on your model:
123+
124+
```php
125+
<?php
126+
127+
namespace App\Models;
128+
129+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
130+
131+
class BlogPost extends Model
132+
{
133+
/**
134+
* The UUID version to use.
135+
*
136+
* @var int
137+
*/
138+
protected $uuidVersion = 1;
139+
}
140+
```
141+
142+
#### Support for `v3` and `v5`
143+
144+
Should you need support for `v3` or `v5` UUIDs, you can simply override the
145+
method which is responsible for generating the UUIDs:
146+
147+
```php
148+
<?php
149+
150+
namespace App\Models;
151+
152+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
153+
use Ramsey\Uuid\Uuid;
154+
155+
class BlogPost extends Model
156+
{
157+
/**
158+
* @throws \Exception
159+
* @return string
160+
*/
161+
protected function generateUuid(): string
162+
{
163+
// UUIDv3 has been used here, but you can also use UUIDv5.
164+
return Uuid::uuid3(Uuid::NAMESPACE_DNS, 'example.com')->toString();
165+
}
166+
}
167+
```
168+
109169
## Running the tests
110170

111171
To run the test suite you can use the following commands:

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "goldspecdigital/laravel-eloquent-uuid",
3-
"description": "A simple drop-in solution for providing UUIDv4 support for the IDs of your Eloquent models.",
3+
"description": "A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.",
44
"type": "library",
55
"license": "MIT",
66
"keywords": [
@@ -41,11 +41,11 @@
4141
},
4242
"scripts": {
4343
"test": [
44-
"@php vendor/bin/php-cs-fixer fix --config=.php_cs --allow-risky=yes --dry-run --diff --verbose --using-cache=no",
45-
"@php vendor/bin/phpunit"
44+
"@test:style",
45+
"@test:unit"
4646
],
47-
"test:style": "@php vendor/bin/php-cs-fixer fix --config=.php_cs --allow-risky=yes --dry-run --diff --verbose --using-cache=no",
47+
"test:style": "@php vendor/bin/php-cs-fixer fix --config=.php_cs --allow-risky=yes --dry-run --diff --verbose",
4848
"test:unit": "@php vendor/bin/phpunit",
49-
"fix:style": "@php vendor/bin/php-cs-fixer fix --config=.php_cs --allow-risky=yes --diff --verbose --using-cache=no"
49+
"fix:style": "@php vendor/bin/php-cs-fixer fix --config=.php_cs --allow-risky=yes --diff --verbose"
5050
}
5151
}

src/Database/Eloquent/Model.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent;
66

7+
use Exception;
78
use Illuminate\Database\Eloquent\Model as BaseModel;
8-
use Illuminate\Support\Str;
9+
use Ramsey\Uuid\Uuid;
910

1011
abstract class Model extends BaseModel
1112
{
@@ -23,6 +24,13 @@ abstract class Model extends BaseModel
2324
*/
2425
protected $keyIsUuid = true;
2526

27+
/**
28+
* The UUID version to use.
29+
*
30+
* @var int
31+
*/
32+
protected $uuidVersion = 4;
33+
2634
/**
2735
* Indicates if the IDs are auto-incrementing.
2836
*
@@ -58,8 +66,24 @@ protected static function boot(): void
5866
static::creating(function (self $model): void {
5967
// Automatically generate a UUID if using them, and not provided.
6068
if ($model->keyIsUuid && empty($model->{$model->getKeyName()})) {
61-
$model->{$model->getKeyName()} = Str::uuid()->toString();
69+
$model->{$model->getKeyName()} = $model->generateUuid();
6270
}
6371
});
6472
}
73+
74+
/**
75+
* @throws \Exception
76+
* @return string
77+
*/
78+
protected function generateUuid(): string
79+
{
80+
switch ($this->uuidVersion) {
81+
case 1:
82+
return Uuid::uuid1()->toString();
83+
case 4:
84+
return Uuid::uuid4()->toString();
85+
}
86+
87+
throw new Exception("UUID version [{$this->uuidVersion}] not supported.");
88+
}
6589
}

tests/Database/Eloquent/ModelWithUuidTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,51 @@
44

55
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
66

7-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid;
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4;
89
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
910
use Illuminate\Support\Str;
1011

1112
class ModelWithUuidTest extends TestCase
1213
{
1314
/** @test */
14-
public function it_generates_a_uuid_when_the_id_has_not_been_set(): void
15+
public function it_generates_a_uuid1_when_the_id_has_not_been_set(): void
1516
{
16-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid $testModel */
17-
$testModel = TestModelWithUuid::query()->create();
17+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1 $testModel */
18+
$testModel = TestModelWithUuid1::query()->create();
1819

1920
$this->assertEquals(36, mb_strlen($testModel->id));
2021
}
2122

2223
/** @test */
23-
public function it_uses_the_uuid_provided_when_id_has_been_set(): void
24+
public function it_generates_a_uuid4_when_the_id_has_not_been_set(): void
25+
{
26+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4 $testModel */
27+
$testModel = TestModelWithUuid4::query()->create();
28+
29+
$this->assertEquals(36, mb_strlen($testModel->id));
30+
}
31+
32+
/** @test */
33+
public function it_uses_the_uuid1_provided_when_id_has_been_set(): void
34+
{
35+
$uuid = Str::uuid()->toString();
36+
37+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1 $testModel */
38+
$testModel = TestModelWithUuid1::query()->create([
39+
'id' => $uuid,
40+
]);
41+
42+
$this->assertEquals($uuid, $testModel->id);
43+
}
44+
45+
/** @test */
46+
public function it_uses_the_uuid4_provided_when_id_has_been_set(): void
2447
{
2548
$uuid = Str::uuid()->toString();
2649

27-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid $testModel */
28-
$testModel = TestModelWithUuid::query()->create([
50+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4 $testModel */
51+
$testModel = TestModelWithUuid4::query()->create([
2952
'id' => $uuid,
3053
]);
3154

tests/Database/Eloquent/ModelWithoutUuidTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44

55
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
66

7-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid;
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid4;
89
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
910
use PDOException;
1011

1112
class ModelWithoutUuidTest extends TestCase
1213
{
1314
/** @test */
14-
public function it_does_not_generate_a_uuid_when_no_id_has_been_set(): void
15+
public function it_does_not_generate_a_uuid1_when_no_id_has_been_set(): void
1516
{
1617
$this->expectException(PDOException::class);
1718

18-
TestModelWithoutUuid::query()->create();
19+
TestModelWithoutUuid1::query()->create();
20+
}
21+
22+
/** @test */
23+
public function it_does_not_generate_a_uuid4_when_no_id_has_been_set(): void
24+
{
25+
$this->expectException(PDOException::class);
26+
27+
TestModelWithoutUuid4::query()->create();
1928
}
2029
}

tests/Models/TestModelWithUuid.php

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Models;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
9+
class TestModelWithUuid1 extends Model
10+
{
11+
/**
12+
* The table associated with the model.
13+
*
14+
* @var string
15+
*/
16+
protected $table = 'test_model_with_uuids';
17+
18+
/**
19+
* The UUID version to use.
20+
*
21+
* @var int
22+
*/
23+
protected $uuidVersion = 1;
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Models;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
9+
class TestModelWithUuid4 extends Model
10+
{
11+
/**
12+
* The table associated with the model.
13+
*
14+
* @var string
15+
*/
16+
protected $table = 'test_model_with_uuids';
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Models;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
9+
class TestModelWithoutUuid1 extends Model
10+
{
11+
/**
12+
* The table associated with the model.
13+
*
14+
* @var string
15+
*/
16+
protected $table = 'test_model_without_uuids';
17+
18+
/**
19+
* Indicates if the IDs are UUIDs.
20+
*
21+
* @var bool
22+
*/
23+
protected $keyIsUuid = false;
24+
25+
/**
26+
* The UUID version to use.
27+
*
28+
* @var int
29+
*/
30+
protected $uuidVersion = 1;
31+
}

0 commit comments

Comments
 (0)