Skip to content

Commit

Permalink
Merge pull request #12 from rawilk/feature/uuid-generator
Browse files Browse the repository at this point in the history
Add uuid generator
  • Loading branch information
rawilk authored Mar 6, 2024
2 parents 3b236fd + 7a0562a commit d948302
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/human-keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| Supported:
| - ksuid (abc_p6UEyCc8D8ecLijAI5zVwOTP3D0)
| - snowflake (abc_1537200202186752)
| - uuid (abc_b8a34e34553a41b885ae218ae81abd42)
|
| Note: Custom generators must implement the contract
| Rawilk\HumanKeys\Contracts\Generator
Expand Down
19 changes: 19 additions & 0 deletions src/Generators/UuidGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rawilk\HumanKeys\Generators;

use Illuminate\Support\Str;
use Rawilk\HumanKeys\Contracts\Generator;

class UuidGenerator implements Generator
{
public function generate(?string $prefix = null): string
{
return implode('_', [
$prefix,
str_replace('-', '', Str::uuid()),
]);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/UuidGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Str;
use Rawilk\HumanKeys\Generators\UuidGenerator;
use Rawilk\HumanKeys\Tests\Fixture\Models\Post;

beforeEach(function () {
config([
'human-keys.generator' => UuidGenerator::class,
]);
});

it('can generate uuid ids for a model', function () {
$uuid = str_replace('-', '', (string) Str::uuid());

Str::createUuidsUsing(fn () => $uuid);

$post = Post::create(['name' => 'Example post']);

expect($post)
->id->toBe("pos_{$uuid}");

Str::createUuidsNormally();
});

0 comments on commit d948302

Please sign in to comment.