44
55namespace Cycle \ORM \Entity \Behavior \Identifier ;
66
7- use Cycle \Database \DatabaseInterface ;
87use Cycle \ORM \Entity \Behavior \Identifier \Snowflake as BaseSnowflake ;
98use Cycle \ORM \Entity \Behavior \Identifier \Listener \SnowflakeGeneric as Listener ;
109use Doctrine \Common \Annotations \Annotation \NamedArgumentConstructor ;
1110use Ramsey \Identifier \Snowflake \Epoch ;
11+ use Ramsey \Identifier \Snowflake \GenericSnowflake ;
1212use Ramsey \Identifier \Snowflake \GenericSnowflakeFactory ;
1313
1414/**
1515 * A distributed ID generation system developed by Twitter that produces
1616 * 64-bit unique, sortable identifiers
1717 *
18+ * Use {@see Listener::setDefaults()} to set default node and epoch offset.
19+ *
1820 * @Annotation
1921 * @NamedArgumentConstructor()
2022 * @Target({"CLASS"})
@@ -28,8 +30,6 @@ final class SnowflakeGeneric extends BaseSnowflake
2830 * @param int<0, 1023>|null $node A node identifier to use when creating Snowflakes
2931 * @param Epoch|int|null $epochOffset The offset from the Unix Epoch in milliseconds
3032 * @param bool $nullable Indicates whether to generate a new Snowflake or not
31- *
32- * @see \Ramsey\Identifier\Snowflake\GenericSnowflakeFactory::create()
3333 */
3434 public function __construct (
3535 string $ field = 'snowflake ' ,
@@ -43,16 +43,28 @@ public function __construct(
4343 $ this ->nullable = $ nullable ;
4444 }
4545
46- #[\Override]
47- public static function fromInteger (
46+ /**
47+ * Identifier factory method from an existing identifier value.
48+ *
49+ * @param int<0, max>|numeric-string $identifier The identifier to create the Snowflake from
50+ * @param int $epochOffset The offset from the Unix Epoch in milliseconds
51+ *
52+ * @see GenericSnowflakeFactory::create()
53+ */
54+ public static function create (
4855 int |string $ identifier ,
49- DatabaseInterface $ database ,
50- array $ arguments ,
51- ): \Ramsey \Identifier \Snowflake {
52- return (new GenericSnowflakeFactory (
53- $ arguments ['node ' ],
54- $ arguments ['epochOffset ' ],
55- ))->createFromInteger ($ identifier );
56+ int $ epochOffset ,
57+ ): GenericSnowflake {
58+ return new GenericSnowflake ($ identifier , $ epochOffset );
59+ }
60+
61+ #[\Override]
62+ protected function getTypecast (): array
63+ {
64+ $ epochOffset = $ this ->epochOffset ?? Listener::getEpochOffset ();
65+ $ epochOffset instanceof Epoch and $ epochOffset = $ epochOffset ->value ;
66+
67+ return [self ::class, 'create ' , [$ epochOffset ]];
5668 }
5769
5870 #[\Override]
@@ -79,13 +91,4 @@ protected function getListenerArgs(): array
7991 'nullable ' => $ this ->nullable ,
8092 ];
8193 }
82-
83- #[\Override]
84- protected function getTypecastArgs (): array
85- {
86- return [
87- 'node ' => $ this ->node ,
88- 'epochOffset ' => $ this ->epochOffset ,
89- ];
90- }
9194}
0 commit comments