1 | <?php |
||
17 | class SnowflakeGenerator implements BinaryGenerator |
||
18 | { |
||
19 | /** |
||
20 | * TODO use private const after drop PHP < 7.1. |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | private static $DATA_CENTER_LENGTH = 5; // data center value 0-31 |
||
25 | |||
26 | /** |
||
27 | * TODO use private const after drop PHP < 7.1. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | private static $MACHINE_LENGTH = 7; // machine value 0-127 |
||
32 | |||
33 | /** |
||
34 | * TODO use private const after drop PHP < 7.1. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | private static $SEQUENCE_LENGTH = 6; // sequence value 0-63 |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $data_center; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $machine; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $time_offset; |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | private $last_time = 0; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | private $sequence = 0; |
||
64 | |||
65 | /** |
||
66 | * Snowflake. |
||
67 | * |
||
68 | * The time offset allows to move the starting point of time in microseconds, |
||
69 | * which reduces the size of the stored time: |
||
70 | * 0 = 1970-01-01 00:00:00 (UTC) |
||
71 | * 1577833200000 = 2020-01-01 00:00:00 (UTC) |
||
72 | * |
||
73 | * @param int $data_center |
||
74 | * @param int $machine |
||
75 | * @param int $time_offset |
||
76 | */ |
||
77 | 13 | public function __construct($data_center, $machine, $time_offset = 0) |
|
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | 4 | public function generate() |
|
147 | } |
||
148 |