1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Ennexa \Snowflake ;
4
6
5
- use Exception \InvalidArgumentException ;
6
- use Exception \InvalidSystemClockException ;
7
+ use Ennexa \ Snowflake \ Exception \InvalidArgumentException ;
8
+ use Ennexa \ Snowflake \ Exception \InvalidSystemClockException ;
7
9
8
- class Generator {
10
+ class Generator
11
+ {
9
12
const NODE_LEN = 8 ;
10
13
const WORKER_LEN = 8 ;
11
14
const SEQUENCE_LEN = 8 ;
12
15
13
- private $ instanceId = 0 ;
14
- private $ startEpoch = 1546300800000 ;
15
- private $ sequenceMax ;
16
- private $ store ;
16
+ private int $ instanceId = 0 ;
17
+ private int $ startEpoch = 1546300800000 ;
18
+ private int $ sequenceMask ;
19
+ private int $ sequenceMax ;
20
+ private StoreInterface $ store ;
21
+ private int $ tickShift ;
17
22
18
- private static function getMaxValue (int $ len )
23
+ private static function getMaxValue (int $ len ): int
19
24
{
20
25
return -1 ^ (-1 << $ len );
21
26
}
22
27
23
28
public static function generateInstanceId ($ nodeId = 0 , $ workerId = 0 )
24
29
{
25
- $ nodeIdMax = $ this -> getMaxValue (self ::NODE_LEN );
30
+ $ nodeIdMax = self :: getMaxValue (self ::NODE_LEN );
26
31
if ($ nodeId < 0 || $ nodeId > $ nodeIdMax ) {
27
- throw InvalidArgumentException ("Node ID should be between 0 and $ nodeIdMax " );
32
+ throw new InvalidArgumentException ("Node ID should be between 0 and $ nodeIdMax " );
28
33
}
29
34
30
- $ workerIdMax = $ this -> getMaxValue (self ::WORKER_LEN );
35
+ $ workerIdMax = self :: getMaxValue (self ::WORKER_LEN );
31
36
if ($ workerId < 0 || $ workerId > $ workerIdMax ) {
32
- throw InvalidArgumentException ("Worker ID should be between 0 and $ workerIdMax " );
37
+ throw new InvalidArgumentException ("Worker ID should be between 0 and $ workerIdMax " );
33
38
}
34
39
35
40
return $ nodeId << self ::WORKER_LEN | $ workerId ;
@@ -51,33 +56,24 @@ public function __construct(StoreInterface $store, int $instanceId = 0, ?int $st
51
56
52
57
/**
53
58
* Set the sequence store
54
- *
55
- * @param int Instance Id
56
- * @return void
57
59
*/
58
- public function setStore (StoreInterface $ store )
60
+ public function setStore (StoreInterface $ store ): void
59
61
{
60
62
$ this ->store = $ store ;
61
63
}
62
64
63
65
/**
64
66
* Get the current generator instance id
65
- *
66
- * @param int Instance Id
67
- * @return void
68
67
*/
69
- public function getInstanceId ()
68
+ public function getInstanceId (): int
70
69
{
71
70
return $ this ->instanceId >> self ::SEQUENCE_LEN ;
72
71
}
73
72
74
73
/**
75
74
* Set the instance id for the generator instance
76
- *
77
- * @param int Instance Id
78
- * @return void
79
75
*/
80
- public function setInstanceId (int $ instanceId )
76
+ public function setInstanceId (int $ instanceId ): void
81
77
{
82
78
$ this ->instanceId = $ instanceId << self ::SEQUENCE_LEN ;
83
79
}
@@ -95,10 +91,10 @@ public function nextSequence()
95
91
/**
96
92
* Generate a unique id based on the epoch and instance id
97
93
*
98
- * @return int unique 64-bit id
94
+ * @return string unique 64-bit id
99
95
* @throws InvalidSystemClockException
100
96
*/
101
- public function nextId ()
97
+ public function nextId (): string
102
98
{
103
99
list ($ timestamp , $ sequence ) = $ this ->nextSequence ();
104
100
0 commit comments