Skip to content

Commit 0042b8f

Browse files
Merge pull request #41 from michaeldyrynda/feature/ordered-uuid-support
Add support for time-ordered UUIDs introduced in Laravel 5.6
2 parents 2370443 + 858cf95 commit 0042b8f

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

.travis.yml

-20
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,8 @@ cache:
66

77
matrix:
88
include:
9-
- php: 5.6.4
10-
env: ILLUMINATE_VERSION=5.3.*
11-
- php: 5.6.4
12-
env: ILLUMINATE_VERSION=5.4.*
13-
- php: 7.0
14-
env: ILLUMINATE_VERSION=5.3.*
15-
- php: 7.0
16-
env: ILLUMINATE_VERSION=5.4.*
17-
- php: 7.0
18-
env: ILLUMINATE_VERSION=5.5.*
19-
- php: 7.1
20-
env: ILLUMINATE_VERSION=5.3.*
21-
- php: 7.1
22-
env: ILLUMINATE_VERSION=5.4.*
23-
- php: 7.1
24-
env: ILLUMINATE_VERSION=5.5.*
259
- php: 7.1
2610
env: ILLUMINATE_VERSION=5.6.*
27-
- php: 7.2
28-
env: ILLUMINATE_VERSION=5.4.*
29-
- php: 7.2
30-
env: ILLUMINATE_VERSION=5.5.*
3111
- php: 7.2
3212
env: ILLUMINATE_VERSION=5.6.*
3313

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Laravel Model UUIDs
2-
## v3.1.0
2+
## v4.0.0
33

44
[![Build Status](https://travis-ci.org/michaeldyrynda/laravel-model-uuid.svg?branch=master)](https://travis-ci.org/michaeldyrynda/laravel-model-uuid)
55
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/?branch=master)
@@ -19,9 +19,7 @@ For more information, check out [this post](https://www.percona.com/blog/2014/12
1919

2020
Take a look at [laravel-efficient-uuid](https://github.com/michaeldyrynda/laravel-efficient-uuid) if you want to make it easy to generate migrations that efficiently store UUID in your database.
2121

22-
This package supports Laravel 5.5 as of version 3.0.0.
23-
24-
This package supports Laravel 5.6 as of version 3.1.0.
22+
This package supports time-ordered UUIDs in Laravel 5.6 as of version 4.0.0.
2523

2624
## Code Samples
2725

@@ -43,7 +41,7 @@ class Post extends Model
4341

4442
It is assumed that you already have a field named `uuid` in your database, which is used to store the generated value.
4543

46-
By default, this package will use UUID version 4 values, however, you are welcome to use `uuid1`, `uuid3`, `uuid4`, or `uuid5` by specifying the protected property `$uuidVersion` in your model.
44+
By default, this package will use UUID version 4 values, however, you are welcome to use `uuid1`, `uuid3`, `uuid4`, or `uuid5` by specifying the protected property `$uuidVersion` in your model. Should you wish to take advantage of ordered UUID (version 4) values that were introduced in Laravel 5.6, you should specify `ordered` as the `$uuidVersion` in your model.
4745

4846
```php
4947
<?php
@@ -116,7 +114,7 @@ public function boot()
116114
This package is installed via [Composer](https://getcomposer.org/). To install, run the following command.
117115

118116
```bash
119-
composer require "dyrynda/laravel-model-uuid:~3.0"
117+
composer require "dyrynda/laravel-model-uuid:~4.0"
120118
```
121119
## Support
122120

composer.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.6.4 || >= 7.0",
20-
"illuminate/database": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
21-
"illuminate/events": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
22-
"ramsey/uuid": "~3.6"
19+
"php": ">= 7.1.3",
20+
"illuminate/database": "5.6.*",
21+
"illuminate/events": "5.6.*",
22+
"illuminate/support": "5.6.*",
23+
"ramsey/uuid": "~3.7",
24+
"moontoast/math": "^1.1"
2325
},
2426
"autoload": {
2527
"psr-4": {
2628
"Dyrynda\\Database\\Support\\": "src/"
2729
}
2830
},
2931
"require-dev": {
30-
"phpunit/phpunit": "~5.7"
32+
"phpunit/phpunit": "~7.0"
3133
},
3234
"autoload-dev": {
3335
"psr-4": {

src/GeneratesUuid.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Dyrynda\Database\Support;
44

55
use Ramsey\Uuid\Uuid;
6+
use Illuminate\Support\Str;
67

78
/**
89
* UUID generation trait.
@@ -29,6 +30,7 @@ trait GeneratesUuid
2930
'uuid3',
3031
'uuid4',
3132
'uuid5',
33+
'ordered',
3234
];
3335

3436
/**
@@ -70,7 +72,11 @@ public static function bootGeneratesUuid()
7072
*/
7173
public function resolveUuid()
7274
{
73-
return call_user_func([Uuid::class, $this->resolveUuidVersion()]);
75+
if (($version = $this->resolveUuidVersion()) == 'ordered') {
76+
return Str::orderedUuid();
77+
}
78+
79+
return call_user_func([Uuid::class, $version]);
7480
}
7581

7682
/**

tests/Feature/UuidTest.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use Tests\Fixtures\Post;
66
use Tests\Fixtures\UncastPost;
7-
use PHPUnit_Framework_TestCase;
7+
use PHPUnit\Framework\TestCase;
8+
use Tests\Fixtures\OrderedPost;
89
use Illuminate\Events\Dispatcher;
910
use Illuminate\Container\Container;
1011
use Illuminate\Database\Capsule\Manager;
1112

12-
class UuidTest extends PHPUnit_Framework_TestCase
13+
class UuidTest extends TestCase
1314
{
1415
public static function setupBeforeClass()
1516
{
@@ -31,7 +32,7 @@ public function it_sets_the_uuid_when_creating_a_new_model()
3132
{
3233
$post = Post::create(['title' => 'Test post']);
3334

34-
$this->assertNotNull($post);
35+
$this->assertNotNull($post->uuid);
3536
}
3637

3738
/** @test */
@@ -87,4 +88,13 @@ public function you_can_find_a_model_by_uuid_without_casting()
8788
$this->assertInstanceOf(UncastPost::class, $post);
8889
$this->assertSame($uuid, $post->uuid);
8990
}
91+
92+
/** @test */
93+
public function it_handles_time_ordered_uuids()
94+
{
95+
$post = OrderedPost::create(['title' => 'test-post']);
96+
97+
$this->assertInstanceOf(OrderedPost::class, $post);
98+
$this->assertNotNull($post->uuid);
99+
}
90100
}

tests/Fixtures/OrderedPost.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
class OrderedPost extends Model
6+
{
7+
protected $uuidVersion = 'ordered';
8+
}

tests/Unit/UuidResolversTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function provider_for_it_handles_uuid_versions()
1919
['uuid4', 'uuid4'],
2020
['uuid5', 'uuid5'],
2121
['uuid999', 'uuid4'],
22+
['ordered', 'ordered'],
2223
];
2324
}
2425

0 commit comments

Comments
 (0)