Skip to content

Commit

Permalink
refactor: improves id management
Browse files Browse the repository at this point in the history
  • Loading branch information
matiux committed Oct 7, 2022
1 parent d0a05a8 commit 6d23386
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/BasicEntityId.php
Expand Up @@ -13,21 +13,25 @@ abstract class BasicEntityId implements EntityId
/**
* @param T $id
*/
final protected function __construct(
protected $id
) {
final protected function __construct(private $id)
{
}

public function equals(EntityId $entityId): bool
{
return $this->id() === $entityId->id();
return $this->value() === $entityId->value();
}

/**
* @return T
*/
public function id()
public function value()
{
return $this->id;
}

public function __toString(): string
{
return (string) $this->value();
}
}
2 changes: 1 addition & 1 deletion src/Matiux/DDDStarterPack/Aggregate/Domain/EntityId.php
Expand Up @@ -23,7 +23,7 @@ public static function createFrom($id): static;
/**
* @return I
*/
public function id();
public function value();

public function equals(EntityId $entityId): bool;
}
Expand Up @@ -19,7 +19,7 @@ abstract class UuidEntityId extends BasicEntityId

public function __toString(): string
{
return !$this->id ? '' : $this->id;
return empty($this->value()) ? '' : $this->value();
}

/**
Expand Down
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DDDStarterPack\Aggregate\Domain;

use Ramsey\Uuid\Uuid;
Expand All @@ -10,4 +12,4 @@ public static function create(): static
{
return new static(Uuid::uuid4()->toString());
}
}
}
5 changes: 3 additions & 2 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/UuidV6EntityId.php
@@ -1,14 +1,15 @@
<?php

declare(strict_types=1);

namespace DDDStarterPack\Aggregate\Domain;

use Ramsey\Uuid\Uuid;

class UuidV6EntityId extends UuidEntityId
{

public static function create(): static
{
return new static(Uuid::uuid6()->toString());
}
}
}
Expand Up @@ -13,7 +13,7 @@ abstract class DoctrineUuidEntityId extends GuidType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value instanceof UuidEntityId ? $value->id() : $value ?? null;
return $value instanceof UuidEntityId ? $value->value() : $value ?? null;
}

public function convertToPHPValue($value, AbstractPlatform $platform)
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function find_person_by_id(): void
self::assertNotNull($person);

$personId = $repo->nextIdentity();
self::assertTrue(UuidEntityId::isValidUuid($personId->id()));
self::assertTrue(UuidEntityId::isValidUuid($personId->value()));
}
}

Expand Down
Expand Up @@ -25,8 +25,8 @@ public function create_specific_uuid(): void
{
$myId = UuidV4EntityId::createFrom('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5');

self::assertEquals('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5', $myId->id());
self::assertIsString($myId->id());
self::assertEquals('79fe4c6b-87f6-4093-98f9-ce6a193ab2a5', $myId->value());
self::assertIsString($myId->value());
}

// /**
Expand Down

0 comments on commit 6d23386

Please sign in to comment.