Skip to content

Commit c946669

Browse files
feature/trait (#22)
* Added trait and tests * Renamed tests * Updated docs * Bumped minor version in README
1 parent 2489811 commit c946669

14 files changed

+453
-26
lines changed

README.md

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ version of Laravel you have installed:
5656
You can install the package via composer:
5757

5858
```bash
59-
composer require goldspecdigital/laravel-eloquent-uuid:^v6.1
59+
composer require goldspecdigital/laravel-eloquent-uuid:^v6.2
6060
```
6161

6262
## Usage
6363

64+
There are two ways to use this package:
65+
1. By extending the provided model classes (preferred and simplest method).
66+
2. By using the provided model trait (allows for extending another model class).
67+
68+
### Extending model
69+
6470
When creating a Eloquent model, instead of extending the standard Laravel model
6571
class, extend from the model class provided by this package:
6672

@@ -77,7 +83,7 @@ class BlogPost extends Model
7783
}
7884
```
7985

80-
### User model
86+
### Extending user model
8187

8288
The User model that comes with a standard Laravel install has some extra
8389
configuration which is implemented in its parent class. This configuration only
@@ -99,13 +105,54 @@ class User extends Authenticatable
99105
}
100106
```
101107

108+
### Using trait
109+
110+
As an alternative to extending the classes in the examples above, you also have
111+
the ability to use the provided trait instead. This requires a more involved
112+
setup process but allows you to extend your models from another class if needed:
113+
114+
```php
115+
<?php
116+
117+
namespace App\Models;
118+
119+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
120+
use Illuminate\Database\Eloquent\Model;
121+
122+
class BlogPost extends Model
123+
{
124+
use Uuid;
125+
126+
/**
127+
* The "type" of the auto-incrementing ID.
128+
*
129+
* @var string
130+
*/
131+
protected $keyType = 'string';
132+
133+
/**
134+
* Indicates if the IDs are auto-incrementing.
135+
*
136+
* @var bool
137+
*/
138+
public $incrementing = false;
139+
140+
/**
141+
* The attributes that are mass assignable.
142+
*
143+
* @var array
144+
*/
145+
protected $guarded = [];
146+
}
147+
```
148+
102149
### Generating UUIDs
103150

104151
If you don't specify the value for the primary key of your model, a UUID will
105152
be automatically generated. However, if you do specify your own UUID then it
106-
will not generate one, but instead use the one you have explicitly provided. This
107-
can be useful when needing the know the ID of the model before you have created
108-
it:
153+
will not generate one, but instead use the one you have explicitly provided.
154+
This can be useful when needing the know the ID of the model before you have
155+
created it:
109156

110157
```php
111158
// No UUID provided (automatically generated).
@@ -120,7 +167,10 @@ echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e
120167
### Specifying UUID versions
121168

122169
By default, `v4` UUIDs will be used for your models. However, you can also
123-
specify `v1` UUIDs to be used by setting the following property on your model:
170+
specify `v1` UUIDs to be used by setting the following property/method on your
171+
model:
172+
173+
#### When extending the class
124174

125175
```php
126176
<?php
@@ -140,6 +190,32 @@ class BlogPost extends Model
140190
}
141191
```
142192

193+
#### When using the trait
194+
195+
```php
196+
<?php
197+
198+
namespace App\Models;
199+
200+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
201+
use Illuminate\Database\Eloquent\Model;
202+
203+
class BlogPost extends Model
204+
{
205+
use Uuid;
206+
207+
/**
208+
* The UUID version to use.
209+
*
210+
* @return int
211+
*/
212+
protected function uuidVersion(): int
213+
{
214+
return 1;
215+
}
216+
}
217+
```
218+
143219
#### Support for `v3` and `v5`
144220

145221
Should you need support for `v3` or `v5` UUIDs, you can simply override the

src/Database/Eloquent/Uuid.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent;
6+
7+
use Exception;
8+
use Ramsey\Uuid\Uuid as RamseyUuid;
9+
10+
trait Uuid
11+
{
12+
/**
13+
* Indicates if the IDs are UUIDs.
14+
*
15+
* @return bool
16+
*/
17+
protected function keyIsUuid(): bool
18+
{
19+
return true;
20+
}
21+
22+
/**
23+
* The UUID version to use.
24+
*
25+
* @return int
26+
*/
27+
protected function uuidVersion(): int
28+
{
29+
return 4;
30+
}
31+
32+
/**
33+
* The "booting" method of the model.
34+
*/
35+
public static function bootUuid(): void
36+
{
37+
static::creating(function (self $model): void {
38+
// Automatically generate a UUID if using them, and not provided.
39+
if ($model->keyIsUuid() && empty($model->{$model->getKeyName()})) {
40+
$model->{$model->getKeyName()} = $model->generateUuid();
41+
}
42+
});
43+
}
44+
45+
/**
46+
* @throws \Exception
47+
* @return string
48+
*/
49+
protected function generateUuid(): string
50+
{
51+
switch ($this->uuidVersion()) {
52+
case 1:
53+
return RamseyUuid::uuid1()->toString();
54+
case 4:
55+
return RamseyUuid::uuid4()->toString();
56+
}
57+
58+
throw new Exception("UUID version [{$this->uuidVersion()}] not supported.");
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithUuid4;
9+
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
10+
use Illuminate\Support\Str;
11+
12+
class ModelExtendingClassWithUuidTest extends TestCase
13+
{
14+
/** @test */
15+
public function it_generates_a_uuid1_when_the_id_has_not_been_set(): void
16+
{
17+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithUuid1 $testModel */
18+
$testModel = TestModelExtendingClassWithUuid1::query()->create();
19+
20+
$this->assertEquals(36, mb_strlen($testModel->id));
21+
}
22+
23+
/** @test */
24+
public function it_generates_a_uuid4_when_the_id_has_not_been_set(): void
25+
{
26+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithUuid4 $testModel */
27+
$testModel = TestModelExtendingClassWithUuid4::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\TestModelExtendingClassWithUuid1 $testModel */
38+
$testModel = TestModelExtendingClassWithUuid1::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
47+
{
48+
$uuid = Str::uuid()->toString();
49+
50+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithUuid4 $testModel */
51+
$testModel = TestModelExtendingClassWithUuid4::query()->create([
52+
'id' => $uuid,
53+
]);
54+
55+
$this->assertEquals($uuid, $testModel->id);
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithoutUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelExtendingClassWithoutUuid4;
9+
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
10+
use PDOException;
11+
12+
class ModelExtendingClassWithoutUuidTest extends TestCase
13+
{
14+
/** @test */
15+
public function it_does_not_generate_a_uuid1_when_no_id_has_been_set(): void
16+
{
17+
$this->expectException(PDOException::class);
18+
19+
TestModelExtendingClassWithoutUuid1::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+
TestModelExtendingClassWithoutUuid4::query()->create();
28+
}
29+
}

tests/Database/Eloquent/ModelWithUuidTest.php renamed to tests/Database/Eloquent/ModelUsingTraitWithUuidTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

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

7-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1;
8-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4;
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid4;
99
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
1010
use Illuminate\Support\Str;
1111

12-
class ModelWithUuidTest extends TestCase
12+
class ModelUsingTraitWithUuidTest extends TestCase
1313
{
1414
/** @test */
1515
public function it_generates_a_uuid1_when_the_id_has_not_been_set(): void
1616
{
17-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1 $testModel */
18-
$testModel = TestModelWithUuid1::query()->create();
17+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid1 $testModel */
18+
$testModel = TestModelUsingTraitWithUuid1::query()->create();
1919

2020
$this->assertEquals(36, mb_strlen($testModel->id));
2121
}
2222

2323
/** @test */
2424
public function it_generates_a_uuid4_when_the_id_has_not_been_set(): void
2525
{
26-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4 $testModel */
27-
$testModel = TestModelWithUuid4::query()->create();
26+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid4 $testModel */
27+
$testModel = TestModelUsingTraitWithUuid4::query()->create();
2828

2929
$this->assertEquals(36, mb_strlen($testModel->id));
3030
}
@@ -34,8 +34,8 @@ public function it_uses_the_uuid1_provided_when_id_has_been_set(): void
3434
{
3535
$uuid = Str::uuid()->toString();
3636

37-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid1 $testModel */
38-
$testModel = TestModelWithUuid1::query()->create([
37+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid1 $testModel */
38+
$testModel = TestModelUsingTraitWithUuid1::query()->create([
3939
'id' => $uuid,
4040
]);
4141

@@ -47,8 +47,8 @@ public function it_uses_the_uuid4_provided_when_id_has_been_set(): void
4747
{
4848
$uuid = Str::uuid()->toString();
4949

50-
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid4 $testModel */
51-
$testModel = TestModelWithUuid4::query()->create([
50+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithUuid4 $testModel */
51+
$testModel = TestModelUsingTraitWithUuid4::query()->create([
5252
'id' => $uuid,
5353
]);
5454

tests/Database/Eloquent/ModelWithoutUuidTest.php renamed to tests/Database/Eloquent/ModelUsingTraitWithoutUuidTest.php

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

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

7-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid1;
8-
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid4;
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithoutUuid1;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelUsingTraitWithoutUuid4;
99
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
1010
use PDOException;
1111

12-
class ModelWithoutUuidTest extends TestCase
12+
class ModelUsingTraitWithoutUuidTest extends TestCase
1313
{
1414
/** @test */
1515
public function it_does_not_generate_a_uuid1_when_no_id_has_been_set(): void
1616
{
1717
$this->expectException(PDOException::class);
1818

19-
TestModelWithoutUuid1::query()->create();
19+
TestModelUsingTraitWithoutUuid1::query()->create();
2020
}
2121

2222
/** @test */
2323
public function it_does_not_generate_a_uuid4_when_no_id_has_been_set(): void
2424
{
2525
$this->expectException(PDOException::class);
2626

27-
TestModelWithoutUuid4::query()->create();
27+
TestModelUsingTraitWithoutUuid4::query()->create();
2828
}
2929
}

tests/Models/TestModelWithUuid1.php renamed to tests/Models/TestModelExtendingClassWithUuid1.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
88

9-
class TestModelWithUuid1 extends Model
9+
class TestModelExtendingClassWithUuid1 extends Model
1010
{
1111
/**
1212
* The table associated with the model.
@@ -15,6 +15,7 @@ class TestModelWithUuid1 extends Model
1515
*/
1616
protected $table = 'test_model_with_uuids';
1717

18+
1819
/**
1920
* The UUID version to use.
2021
*

tests/Models/TestModelWithUuid4.php renamed to tests/Models/TestModelExtendingClassWithUuid4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
88

9-
class TestModelWithUuid4 extends Model
9+
class TestModelExtendingClassWithUuid4 extends Model
1010
{
1111
/**
1212
* The table associated with the model.

0 commit comments

Comments
 (0)