Skip to content

Commit

Permalink
feat: separates uuid v4 and v6 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
matiux committed Oct 6, 2022
1 parent 2b62723 commit d0a05a8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/UuidEntityId.php
Expand Up @@ -11,9 +11,11 @@
/**
* @extends BasicEntityId<string>
*/
class UuidEntityId extends BasicEntityId
abstract class UuidEntityId extends BasicEntityId
{
public const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
// public const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
// See https://github.com/ramsey/uuid/blob/4.x/src/Validator/GenericValidator.php#L32
public const UUID_PATTERN = '/\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\z/';

public function __toString(): string
{
Expand All @@ -25,10 +27,7 @@ public function __toString(): string
*
* @return static
*/
public static function create(): static
{
return new static(Uuid::uuid4()->toString());
}
abstract public static function create(): static;

/**
* @param string $id
Expand Down
13 changes: 13 additions & 0 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/UuidV4EntityId.php
@@ -0,0 +1,13 @@
<?php

namespace DDDStarterPack\Aggregate\Domain;

use Ramsey\Uuid\Uuid;

class UuidV4EntityId extends UuidEntityId
{
public static function create(): static
{
return new static(Uuid::uuid4()->toString());
}
}
14 changes: 14 additions & 0 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/UuidV6EntityId.php
@@ -0,0 +1,14 @@
<?php

namespace DDDStarterPack\Aggregate\Domain;

use Ramsey\Uuid\Uuid;

class UuidV6EntityId extends UuidEntityId
{

public static function create(): static
{
return new static(Uuid::uuid6()->toString());
}
}
4 changes: 2 additions & 2 deletions tests/Support/Model/PersonId.php
Expand Up @@ -4,8 +4,8 @@

namespace Tests\Support\Model;

use DDDStarterPack\Aggregate\Domain\UuidEntityId;
use DDDStarterPack\Aggregate\Domain\UuidV4EntityId;

class PersonId extends UuidEntityId
class PersonId extends UuidV4EntityId
{
}
12 changes: 6 additions & 6 deletions tests/Unit/DDDStarterPack/Aggregate/Domain/UuidEntityIdTest.php
Expand Up @@ -4,7 +4,7 @@

namespace Tests\Unit\DDDStarterPack\Aggregate\Domain;

use DDDStarterPack\Aggregate\Domain\UuidEntityId;
use DDDStarterPack\Aggregate\Domain\UuidV4EntityId;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

Expand All @@ -15,15 +15,15 @@ class UuidEntityIdTest extends TestCase
*/
public function create_randomic_uuid(): void
{
self::assertMatchesRegularExpression(UuidEntityId::UUID_PATTERN, (string) UuidEntityId::create());
self::assertMatchesRegularExpression(UuidV4EntityId::UUID_PATTERN, (string) UuidV4EntityId::create());
}

/**
* @test
*/
public function create_specific_uuid(): void
{
$myId = UuidEntityId::createFrom('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5');
$myId = UuidV4EntityId::createFrom('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5');

self::assertEquals('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5', $myId->id());
self::assertIsString($myId->id());
Expand All @@ -47,15 +47,15 @@ public function it_should_throw_exception_if_input_id_is_invalid(): void
{
self::expectException(InvalidArgumentException::class);

UuidEntityId::createFrom('');
UuidV4EntityId::createFrom('');
}

/**
* @test
*/
public function is_should_verify_if_id_is_in_uuid_v4_format(): void
{
self::assertTrue(UuidEntityId::isValidUuid('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5'));
self::assertFalse(UuidEntityId::isValidUuid('12345'));
self::assertTrue(UuidV4EntityId::isValidUuid('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5'));
self::assertFalse(UuidV4EntityId::isValidUuid('12345'));
}
}

0 comments on commit d0a05a8

Please sign in to comment.