Skip to content

Commit 2132680

Browse files
Feature: Configurable UUID version (#142)
* Feature: Added configuration option for uuid version, defaults to uuid4 and if no configuration option is set then still defaults to uuid4. --------- Co-authored-by: Jamie Fairweather <16147285+zagreusinoz@users.noreply.github.com> Co-authored-by: Michael Dyrynda <michael@dyrynda.com.au>
1 parent 9e6ae37 commit 2132680

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,25 @@ class Post extends Model
8585
{
8686
use GeneratesUuid;
8787

88-
public function uuidVersion(): string
88+
public function uuidVersion(): ?string
8989
{
9090
return 'uuid5';
9191
}
9292
}
9393
```
9494

95+
Alternatively, if you would like to use a consistent `$uuidVersion` on all of your models, you may set the `uuid_version` in your `config/model-uuid.php`.
96+
A non-empty value returned from the model's `uuidVersion(): ?string` method will take priority over the config version.
97+
98+
```php
99+
return [
100+
/**
101+
* The default uuid version used to generate UUID value.
102+
*/
103+
'uuid_version' => 'uuid4',
104+
];
105+
```
106+
95107
Whilst not recommended, if you _do_ choose to use a UUID as your primary model key (`id`), be sure to configure your model for this setup correctly. Not updating these properties will lead to Laravel attempting to convert your `id` column to an integer, which will be cast to `0`. When used in combination with the `EfficientUuid` cast, this casting will result in a `Ramsey\Uuid\Exception\InvalidUuidStringException` being thrown.
96108

97109
```php

config/model-uuid.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
* The default column name which should be used to store the generated UUID value.
66
*/
77
'column_name' => 'uuid',
8+
9+
/**
10+
* The default uuid version used to generate UUID value.
11+
*/
12+
'uuid_version' => 'uuid4',
813
];

src/GeneratesUuid.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,20 @@ public function resolveUuid(): UuidInterface
9292
return call_user_func([Uuid::class, $this->resolveUuidVersion()]);
9393
}
9494

95-
public function uuidVersion(): string
95+
public function uuidVersion(): ?string
9696
{
97-
return 'uuid4';
97+
return null;
9898
}
9999

100100
/**
101101
* Resolve the UUID version to use when setting the UUID value. Default to uuid4.
102102
*/
103103
public function resolveUuidVersion(): string
104104
{
105-
if (($uuidVersion = $this->uuidVersion()) === 'ordered') {
105+
106+
$uuidVersion = $this->uuidVersion() ?? config('model-uuid.uuid_version', 'uuid4');
107+
108+
if ($uuidVersion === 'ordered') {
106109
$uuidVersion = 'uuid6';
107110
}
108111

tests/Feature/GeneratesUuidTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,61 @@ public function it_gets_default_column_name()
3030
'The UUID column should match the configured value.'
3131
);
3232
}
33+
34+
#[Test]
35+
public function it_inherits_uuid_version_from_config()
36+
{
37+
Config::set('model-uuid.uuid_version', 'uuid1');
38+
39+
$testClass = new class
40+
{
41+
use GeneratesUuid;
42+
};
43+
44+
$this->assertSame('uuid1', $testClass->resolveUuidVersion());
45+
}
46+
47+
#[Test]
48+
public function it_defaults_to_uuid4_when_config_not_set()
49+
{
50+
Config::set('model-uuid.uuid_version', null);
51+
52+
$testClass = new class
53+
{
54+
use GeneratesUuid;
55+
};
56+
57+
$this->assertSame('uuid4', $testClass->resolveUuidVersion());
58+
}
59+
60+
#[Test]
61+
public function it_defaults_to_uuid4_when_config_is_empty()
62+
{
63+
Config::set('model-uuid.uuid_version', '');
64+
65+
$testClass = new class
66+
{
67+
use GeneratesUuid;
68+
};
69+
70+
$this->assertSame('uuid4', $testClass->resolveUuidVersion());
71+
}
72+
73+
#[Test]
74+
public function it_uses_model_definition_as_highest_precedence()
75+
{
76+
Config::set('model-uuid.uuid_version', 'uuid7');
77+
78+
$testClass = new class
79+
{
80+
use GeneratesUuid;
81+
82+
public function uuidVersion(): ?string
83+
{
84+
return 'uuid6';
85+
}
86+
};
87+
88+
$this->assertSame('uuid6', $testClass->resolveUuidVersion());
89+
}
3390
}

0 commit comments

Comments
 (0)