ldc-cocotte/shared/DDD/EntityId.php
Jérémy Munsch d1f0b21ad5
refactor(EntityId): move UUID validation to EntityId constructor
Refactored the EntityId class to include UUID v7 validation in the constructor, ensuring all derived classes inherit this behavior.
2026-11-16 11:39:52 +01:00

30 lines
655 B
PHP

<?php
declare(strict_types=1);
namespace Shared\DDD;
use Ramsey\Uuid\UuidInterface;
use Shared\Helpers\UuidHelper;
abstract readonly class EntityId implements \Stringable, Equatable
{
public function __construct(
public UuidInterface $value,
) {
if (! UuidHelper::isV7($this->value)) {
throw new \DomainException('value must be a valid UUID v7');
}
}
#[\Override]
public function __toString(): string
{
return $this->value->toString();
}
public function equals(mixed $other): bool
{
return $other instanceof self && $this->value->equals($other->value);
}
}