<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 工具类:薄雾算法
+-----------------------------------------------------------------------------------------------------------------------
 * PHP version 7
 *
 * @category  App\Util
 * @package   App\Util
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年9月1日15:44:32
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Util;

/*
* 薄雾算法
*
* 1      2                                                     48         56       64
* +------+-----------------------------------------------------+----------+----------+
* retain | increas                                             | salt     | sequence |
* +------+-----------------------------------------------------+----------+----------+
* 0      | 0000000000 0000000000 0000000000 0000000000 0000000 | 00000000 | 00000000 |
* +------+-----------------------------------------------------+------------+--------+
*
* 0. 最高位,占 1 位,保持为 0,使得值永远为正数;
* 1. 自增数,占 47 位,自增数在高位能保证结果值呈递增态势,遂低位可以为所欲为;
* 2. 随机因子一,占 8 位,上限数值 255,使结果值不可预测;
* 3. 随机因子二,占 8 位,上限数值 255,使结果值不可预测;
*
* 编号上限为百万亿级,上限值计算为 140737488355327 即 int64(1 << 47 - 1),假设每天取值 10 亿,能使用 385+ 年
 */

class Mist
{
    const saltBit = 8;  // 随机因子二进制位数

    public const saltShift = 8;        // 随机因子移位数

    public const increasShift = self::saltBit + self::saltShift; // 自增数移位数

    public $increas = 0;                // 自增数

    public $saltA = 0;                  // 随机因子一

    public $saltB = 0;                  // 随机因子二

    public function __construct($increas = 1)
    {
        $this->increas = $increas;
    }

    public function generate(): int
    {
        $this->increas = ++$this->increas;
        // 获取随机因子数值
        try {
            $this->saltA = random_int($this->saltA, 255);
        } catch (\Exception $e) {
            $this->saltA = $this->saltA++;
        }
        try {
            $this->saltB = random_int($this->saltB, 255);
        } catch (\Exception $e) {
            $this->saltB = $this->saltB++;
        }
        return (int) ($this->increas << self::increasShift) | ($this->saltA << self::saltShift) | $this->saltB;
    }
}