summaryrefslogtreecommitdiff
path: root/src/Manager.php
blob: 215ec50ec51ba80b88e3bacd29fd01d23f55580d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

declare(strict_types=1);

namespace Zhineng\Snowflake;

final class Manager
{
    /**
     * The ID format.
     */
    private Structure $structure;

    /**
     * The last value except sequence.
     */
    private int $lastCombination = 0;

    /**
     * Set the ID format.
     */
    public function structureUsing(Structure $structure): self
    {
        $this->structure = $structure;
        $this->lastCombination = 0;

        return $this;
    }

    /**
     * Generate the next Snowflake ID.
     */
    public function nextId(): int
    {
        if (! isset($this->structure)) {
            throw new \RuntimeException('ID structure is not defined.');
        }

        $id = 0;
        $sequence = null;

        foreach ($this->structure->components() as $field) {
            // Defer sequence until after detecting other-field changes
            // so we can reset it if needed.
            if ($field instanceof Sequence) {
                $sequence = $field;

                continue;
            }

            $current = $field->maxValue() & $field->value();
            $id |= $current << $field->offset();
        }

        // The ID variable now holds the combination of all fields except
        // sequence. Reset the sequence field if any other field has changed.
        if ($this->lastCombination !== $id) {
            $sequence?->reset();
        }

        $this->lastCombination = $id;

        if ($sequence instanceof Sequence) {
            $id |= ($sequence->maxValue() & $sequence->value()) << $sequence->offset();
        }

        return $id;
    }
}