Skip to content

Commit 578dba6

Browse files
authored
Feature/Added support or Ordered UUID. (#18)
1 parent 4be8d68 commit 578dba6

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/ValueObjects/UUIDValue.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
use ComplexHeart\Domain\Contracts\Model\Identifier;
99
use Exception;
10+
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
11+
use Ramsey\Uuid\Generator\CombGenerator;
1012
use Ramsey\Uuid\Uuid;
13+
use Ramsey\Uuid\UuidFactory;
1114

1215
/**
1316
* Class UUIDValue
@@ -17,8 +20,6 @@
1720
*/
1821
class UUIDValue extends Value implements Identifier
1922
{
20-
private const RANDOM = 'random';
21-
2223
/**
2324
* The uuid string value.
2425
*
@@ -33,9 +34,7 @@ class UUIDValue extends Value implements Identifier
3334
*/
3435
final public function __construct(string $value)
3536
{
36-
$this->initialize(
37-
['value' => ($value === self::RANDOM) ? Uuid::uuid4()->toString() : $value]
38-
);
37+
$this->initialize(['value' => $value]);
3938
}
4039

4140
/**
@@ -64,9 +63,26 @@ public function value(): string
6463
* @return static
6564
* @throws Exception
6665
*/
67-
public static function random(): self
66+
public static function random(bool $ordered = true): self
6867
{
69-
return new static(self::RANDOM);
68+
if ($ordered) {
69+
$factory = new UuidFactory;
70+
71+
$factory->setRandomGenerator(new CombGenerator(
72+
$factory->getRandomGenerator(),
73+
$factory->getNumberConverter()
74+
));
75+
76+
$factory->setCodec(new TimestampFirstCombCodec(
77+
$factory->getUuidBuilder()
78+
));
79+
80+
$uuid = $factory->uuid4();
81+
} else {
82+
$uuid = Uuid::uuid4();
83+
}
84+
85+
return new static($uuid->toString());
7086
}
7187

7288
/**

0 commit comments

Comments
 (0)