Skip to content

Commit b82b313

Browse files
fix: make it possible to set datacenter and workerId intentionally to 0 (#81)
With the change in f0dbd16 it's impossible to intentionally set the datacenter or workerId to 0. This change changes the default value to -1 which then sets a random value.
1 parent f0dbd16 commit b82b313

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Snowflake.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ class Snowflake
5454
/**
5555
* Build Snowflake Instance.
5656
*/
57-
public function __construct(int $datacenter = 0, int $workerId = 0)
57+
public function __construct(int $datacenter = -1, int $workerId = -1)
5858
{
5959
$maxDataCenter = -1 ^ (-1 << self::MAX_DATACENTER_LENGTH);
6060
$maxWorkId = -1 ^ (-1 << self::MAX_WORKID_LENGTH);
6161

6262
// If not set datacenter or workid, we will set a default value to use.
63-
$this->datacenter = $datacenter > $maxDataCenter || $datacenter <= 0 ? random_int(0, 31) : $datacenter;
64-
$this->workerId = $workerId > $maxWorkId || $workerId <= 0 ? random_int(0, 31) : $workerId;
63+
$this->datacenter = $datacenter > $maxDataCenter || $datacenter < 0 ? random_int(0, 31) : $datacenter;
64+
$this->workerId = $workerId > $maxWorkId || $workerId < 0 ? random_int(0, 31) : $workerId;
6565
}
6666

6767
/**

0 commit comments

Comments
 (0)