|
4 | 4 |
|
5 | 5 | namespace Cycle\ORM\Entity\Behavior\Identifier\Listener; |
6 | 6 |
|
7 | | -use Cycle\ORM\Entity\Behavior\Attribute\Listen; |
8 | | -use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate; |
9 | | -use Ramsey\Identifier\Uuid\UuidFactory; |
| 7 | +use Ramsey\Identifier\Uuid\UuidV1Factory; |
10 | 8 |
|
11 | | -final class Uuid1 |
| 9 | +/** |
| 10 | + * Generates UUIDv1 identifiers for entities. |
| 11 | + * You can set default node and clock sequence using the {@see setDefaults()} method. |
| 12 | + */ |
| 13 | +final class Uuid1 extends BaseUuid |
12 | 14 | { |
| 15 | + /** @var int<0, 281474976710655>|non-empty-string|null */ |
| 16 | + private static int|string|null $defaultNode = null; |
| 17 | + |
| 18 | + private static ?int $defaultClockSeq = null; |
| 19 | + private UuidV1Factory $factory; |
| 20 | + |
13 | 21 | /** |
14 | | - * @param int<0, 281474976710655>|non-empty-string|null $node |
| 22 | + * @param non-empty-string $field The name of the field to store the UUID |
| 23 | + * @param bool $nullable Indicates whether the UUID can be null |
| 24 | + * @param int<0, 281474976710655>|non-empty-string|null $node A 48-bit integer or hexadecimal string representing the hardware address |
| 25 | + * @param int|null $clockSeq A number used to help avoid duplicates that could arise when the clock is set backwards in time |
15 | 26 | */ |
16 | 27 | public function __construct( |
17 | | - private string $field = 'uuid', |
18 | | - private int|string|null $node = null, |
19 | | - private ?int $clockSeq = null, |
20 | | - private bool $nullable = false, |
21 | | - ) {} |
22 | | - |
23 | | - #[Listen(OnCreate::class)] |
24 | | - public function __invoke(OnCreate $event): void |
25 | | - { |
26 | | - if ($this->nullable || isset($event->state->getData()[$this->field])) { |
27 | | - return; |
28 | | - } |
| 28 | + string $field, |
| 29 | + bool $nullable = false, |
| 30 | + private readonly int|string|null $node = null, |
| 31 | + private readonly ?int $clockSeq = null, |
| 32 | + ) { |
| 33 | + $this->factory = new UuidV1Factory(); |
| 34 | + parent::__construct($field, $nullable); |
| 35 | + } |
29 | 36 |
|
30 | | - $identifier = (new UuidFactory())->v1( |
31 | | - $this->node, |
32 | | - $this->clockSeq, |
33 | | - ); |
| 37 | + /** |
| 38 | + * Set default node and clock sequence for UUIDv1 generation. |
| 39 | + * |
| 40 | + * @param int<0, 281474976710655>|non-empty-string|null $node The node to set |
| 41 | + * @param int|null $clockSeq The clock sequence to set |
| 42 | + */ |
| 43 | + public static function setDefaults(int|string|null $node, ?int $clockSeq): void |
| 44 | + { |
| 45 | + self::$defaultNode = $node; |
| 46 | + self::$defaultClockSeq = $clockSeq; |
| 47 | + } |
34 | 48 |
|
35 | | - $event->state->register($this->field, $identifier); |
| 49 | + #[\Override] |
| 50 | + protected function createValue(): \Ramsey\Identifier\Uuid |
| 51 | + { |
| 52 | + $node = $this->node ?? self::$defaultNode; |
| 53 | + $clockSeq = $this->clockSeq ?? self::$defaultClockSeq; |
| 54 | + return $this->factory->create($node, $clockSeq); |
36 | 55 | } |
37 | 56 | } |
0 commit comments