Skip to content

Feature/Added support or Ordered UUID. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/ValueObjects/UUIDValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

use ComplexHeart\Domain\Contracts\Model\Identifier;
use Exception;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;

/**
* Class UUIDValue
Expand All @@ -17,8 +20,6 @@
*/
class UUIDValue extends Value implements Identifier
{
private const RANDOM = 'random';

/**
* The uuid string value.
*
Expand All @@ -33,9 +34,7 @@ class UUIDValue extends Value implements Identifier
*/
final public function __construct(string $value)
{
$this->initialize(
['value' => ($value === self::RANDOM) ? Uuid::uuid4()->toString() : $value]
);
$this->initialize(['value' => $value]);
}

/**
Expand Down Expand Up @@ -64,9 +63,26 @@ public function value(): string
* @return static
* @throws Exception
*/
public static function random(): self
public static function random(bool $ordered = true): self
{
return new static(self::RANDOM);
if ($ordered) {
$factory = new UuidFactory;

$factory->setRandomGenerator(new CombGenerator(
$factory->getRandomGenerator(),
$factory->getNumberConverter()
));

$factory->setCodec(new TimestampFirstCombCodec(
$factory->getUuidBuilder()
));

$uuid = $factory->uuid4();
} else {
$uuid = Uuid::uuid4();
}

return new static($uuid->toString());
}

/**
Expand Down
Loading