diff --git a/src/Snowflake.php b/src/Snowflake.php index 2c97380..78fbf39 100644 --- a/src/Snowflake.php +++ b/src/Snowflake.php @@ -63,7 +63,7 @@ class Snowflake public function __construct(int $timestamp = null, int $workerId = 1, int $datacenterId = 1) { if ($timestamp === null) { - $timestamp = strtotime(self::DEFAULT_EPOCH_DATETIME); + $timestamp = strtotime(config('snowflake.epoch', '') ?: self::DEFAULT_EPOCH_DATETIME); } $this->epoch = $timestamp; @@ -72,6 +72,39 @@ public function __construct(int $timestamp = null, int $workerId = 1, int $datac $this->lastTimestamp = $this->epoch; } + /** + * + * 创建实例化 + * + * @return int + */ + public static function make() + { + return (new static); + } + + /** + * 创建无符号bigint 64bit 唯一Id 大约可用69年. + * timestamp_bits(41) + datacenter_id_bits(5) + worker_id_bits(5) + sequence_bits(12) + * + * @return int + */ + public static function nextId(): int + { + return self::make()->id(); + } + + /** + * 创建兼容JS 53bit 唯一Id 大约可用68年. + * timestamp_bits(31) + datacenter_id_bits(5) + worker_id_bits(5) + sequence_bits(12) + * + * @return int + */ + public static function shortId(): int + { + return self::make()->short(); + } + public function makeSequenceId(int $currentTime, int $max = self::MAX_SEQUENCE): int { if ($this->lastTimestamp === $currentTime) { @@ -102,17 +135,6 @@ public function id(): int return $this->toSnowflakeId($currentTime - $this->epoch * 1000, $sequenceId); } - /** - * 创建无符号bigint 64bit 唯一Id 大约可用69年. - * timestamp_bits(41) + datacenter_id_bits(5) + worker_id_bits(5) + sequence_bits(12) - * - * @return int - */ - public static function nextId(): int - { - return (new static)->id(); - } - /** * 创建兼容JS 53bit 唯一Id 大约可用68年. * timestamp_bits(31) + datacenter_id_bits(5) + worker_id_bits(5) + sequence_bits(12) @@ -157,6 +179,11 @@ public function timestamp($microtime=true): int } } + public function shortParse(int $id): array + { + return $this->parse($id, true); + } + public function parse(int $id, bool $is_short=false): array { $id = decbin($id); @@ -190,4 +217,4 @@ public function parse(int $id, bool $is_short=false): array 'datetime' => $datetime, ]; } -} +} \ No newline at end of file