Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 79c4ede

Browse files
authored
Refactor (#2)
* Use ramsey/uuid * Improve test coverage.
1 parent 97dd437 commit 79c4ede

File tree

7 files changed

+64
-46
lines changed

7 files changed

+64
-46
lines changed

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@
5454
"laravel": {
5555
"providers": [
5656
"Bmatovu\\Uuid\\UuidServiceProvider"
57-
],
58-
"aliases": {
59-
"Uuid": "Bmatovu\\Uuid\\UuidFacade"
60-
}
57+
]
6158
}
6259
}
6360
}

config/uuid.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,18 @@
1010
| http://tools.ietf.org/html/rfc4122
1111
*/
1212
'version' => env('UUID_VERSION', 4),
13+
14+
/*
15+
|--------------------------------------------------------------------------
16+
| UUID Namespace
17+
|--------------------------------------------------------------------------
18+
*/
19+
'namespace' => env('UUID_NAMESPACE'),
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| UUID Name
24+
|--------------------------------------------------------------------------
25+
*/
26+
'name' => env('UUID_NAMESPACE'),
1327
];

src/Uuid.php renamed to src/Support/Util.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

3-
namespace Bmatovu\Uuid;
3+
namespace Bmatovu\Uuid\Support;
44

5-
use Ramsey\Uuid\Uuid as RamseyUuid;
5+
use Ramsey\Uuid\Exception\InvalidUuidStringException;
6+
use Ramsey\Uuid\Uuid;
67
use Ramsey\Uuid\UuidInterface;
78

8-
class Uuid
9+
class Util
910
{
1011
/**
1112
* Generate UUID by version.
@@ -18,24 +19,24 @@ class Uuid
1819
*
1920
* @return \Ramsey\Uuid\UuidInterface UUID
2021
*/
21-
public static function generate(int $version = 4, $ns = null, string $name = null): UuidInterface
22+
public static function generateUuid(int $version = 4, $ns = null, string $name = null): UuidInterface
2223
{
23-
$ns = $ns ? $ns : RamseyUuid::NAMESPACE_DNS;
24+
$ns = $ns ? $ns : Uuid::NAMESPACE_DNS;
2425

2526
$name = $name ? $name : php_uname('n');
2627

2728
switch ($version) {
2829
case 1:
29-
$uuid = RamseyUuid::uuid1();
30+
$uuid = Uuid::uuid1();
3031
break;
3132
case 3:
32-
$uuid = RamseyUuid::uuid3($ns, $name);
33+
$uuid = Uuid::uuid3($ns, $name);
3334
break;
3435
case 5:
35-
$uuid = RamseyUuid::uuid5($ns, $name);
36+
$uuid = Uuid::uuid5($ns, $name);
3637
break;
3738
default:
38-
$uuid = RamseyUuid::uuid4();
39+
$uuid = Uuid::uuid4();
3940
break;
4041
}
4142

@@ -45,21 +46,19 @@ public static function generate(int $version = 4, $ns = null, string $name = nul
4546
/**
4647
* Validate UUID plus version.
4748
*
48-
* @param string $uuid
49+
* @param string $str
4950
* @param int|null $version UUID Version
5051
*
5152
* @return bool True for valid.
5253
*/
53-
public static function validate(string $uuid, int $version = null)
54+
public static function validateUuid(string $str, int $version = null)
5455
{
5556
if ($version === null) {
56-
return RamseyUuid::isValid($value);
57+
return Uuid::isValid($str);
5758
}
5859

5960
try {
60-
$uuid = RamseyUuid::fromString($value);
61-
62-
return $uuid->getVersion() === $version;
61+
return Uuid::fromString($str)->getVersion() === $version;
6362
} catch (InvalidUuidStringException $e) {
6463
return false;
6564
}

src/Traits/HasUuidKey.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bmatovu\Uuid\Traits;
44

5-
use Bmatovu\Uuid\Uuid;
5+
use Bmatovu\Uuid\Support\Util;
66
use Illuminate\Database\Eloquent\Model;
77

88
trait HasUuidKey
@@ -39,7 +39,10 @@ protected static function boot(): void
3939
static::creating(function (Model $model): void {
4040
if (! $model->getKey()) {
4141
$version = config('uuid.version');
42-
$model->{$model->getKeyName()} = Uuid::generate($version)->toString();
42+
$namespace = config('uuid.namespace');
43+
$name = config('uuid.name');
44+
45+
$model->{$model->getKeyName()} = Util::generateUuid($version, $namespace, $name)->toString();
4346
}
4447
});
4548
}

src/UuidFacade.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/UuidServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public function boot()
2525
*/
2626
public function register()
2727
{
28-
$this->app->bind('uuid', function () {
29-
return new Uuid();
30-
});
31-
3228
$this->mergeConfigFrom(__DIR__.'/../config/uuid.php', 'uuid');
3329
}
3430
}

tests/Support/UtilTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bmatovu\Uuid\Tests\Support;
4+
5+
use Bmatovu\Uuid\Support\Util;
6+
use Bmatovu\Uuid\Tests\TestCase;
7+
8+
class UtilTest extends TestCase
9+
{
10+
public function test_generate_v1_uuids()
11+
{
12+
foreach ([1,3,4,5] as $version) {
13+
$uuid_v{$version} = Util::generateUuid($version);
14+
15+
$this->assertInstanceOf(\Ramsey\Uuid\Uuid::class, $uuid_v{$version});
16+
$this->assertEquals($version, $uuid_v{$version}->getVersion());
17+
}
18+
}
19+
20+
public function test_validate_uuids()
21+
{
22+
$valid_uuid = Util::generateUuid();
23+
$invalid_uuid = '3f6f8cb0-c57d-11e1-9b21-0800200c9a6';
24+
25+
$this->assertTrue(Util::validateUuid($valid_uuid));
26+
$this->assertTrue(Util::validateUuid($valid_uuid, 4));
27+
28+
$this->assertFalse(Util::validateUuid($invalid_uuid, 1));
29+
}
30+
}

0 commit comments

Comments
 (0)