|
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; |
| 7 | +use Ramsey\Identifier\Snowflake; |
9 | 8 | use Ramsey\Identifier\Snowflake\Epoch; |
10 | 9 | use Ramsey\Identifier\Snowflake\GenericSnowflakeFactory; |
11 | 10 |
|
12 | | -final class SnowflakeGeneric |
| 11 | +/** |
| 12 | + * Generates generic Snowflake identifiers for entities. |
| 13 | + * You can set default node and epoch offset using the {@see setDefaults()} method. |
| 14 | + */ |
| 15 | +final class SnowflakeGeneric extends \Cycle\ORM\Entity\Behavior\Identifier\Listener\Snowflake |
13 | 16 | { |
| 17 | + /** @var int<0, 1023> */ |
| 18 | + private static int $node = 0; |
| 19 | + |
| 20 | + /** @var Epoch|int */ |
| 21 | + private static Epoch|int $epochOffset = 0; |
| 22 | + |
| 23 | + private GenericSnowflakeFactory $factory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param non-empty-string $field The name of the field to store the Snowflake identifier |
| 27 | + * @param bool $nullable Indicates whether the Snowflake identifier can be null |
| 28 | + * @param int<0, 1023>|null $node A node identifier to use when creating Snowflakes |
| 29 | + * @param Epoch|int|null $epochOffset The offset from the Unix Epoch in milliseconds |
| 30 | + */ |
14 | 31 | public function __construct( |
15 | | - private string $field = 'snowflake', |
16 | | - private int $node = 0, |
17 | | - private Epoch|int $epochOffset = 0, |
18 | | - private bool $nullable = false, |
19 | | - ) {} |
20 | | - |
21 | | - #[Listen(OnCreate::class)] |
22 | | - public function __invoke(OnCreate $event): void |
| 32 | + string $field, |
| 33 | + bool $nullable = false, |
| 34 | + ?int $node = null, |
| 35 | + Epoch|int|null $epochOffset = null, |
| 36 | + ) { |
| 37 | + $node ??= self::$node; |
| 38 | + $epochOffset ??= self::$epochOffset; |
| 39 | + $this->factory = new GenericSnowflakeFactory($node, $epochOffset); |
| 40 | + parent::__construct($field, $nullable); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Set default node and epoch offset for Snowflake generation. |
| 45 | + * |
| 46 | + * @param null|int<0, 1023> $node The node ID to set. Null to use the default (0). |
| 47 | + * @param Epoch|int|null $epochOffset The epoch offset to set. Null to use the default (0). |
| 48 | + */ |
| 49 | + public static function setDefaults(?int $node, Epoch|int|null $epochOffset): void |
23 | 50 | { |
24 | | - if ($this->nullable || isset($event->state->getData()[$this->field])) { |
25 | | - return; |
| 51 | + if ($node !== null && ($node < 0 || $node > 1023)) { |
| 52 | + throw new \InvalidArgumentException('Node ID must be between 0 and 1023.'); |
26 | 53 | } |
27 | 54 |
|
28 | | - $identifier = (new GenericSnowflakeFactory($this->node, $this->epochOffset))->create(); |
| 55 | + self::$node = (int) $node; |
| 56 | + if ($epochOffset !== null) { |
| 57 | + self::$epochOffset = $epochOffset; |
| 58 | + } |
| 59 | + } |
29 | 60 |
|
30 | | - $event->state->register($this->field, $identifier); |
| 61 | + #[\Override] |
| 62 | + protected function createValue(): Snowflake |
| 63 | + { |
| 64 | + return $this->factory->create(); |
31 | 65 | } |
32 | 66 | } |
0 commit comments