diff options
| author | Zhineng Li <[email protected]> | 2026-01-05 16:26:11 +0800 |
|---|---|---|
| committer | Zhineng Li <[email protected]> | 2026-01-05 16:26:11 +0800 |
| commit | a49763dd739c3c68c4a8322896d594e926ac8e6b (patch) | |
| tree | ab96cf55ccf828ec3c61d7cf5c440cc8fe837a9b /src/Manager.php | |
first commit
Diffstat (limited to 'src/Manager.php')
| -rw-r--r-- | src/Manager.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/Manager.php b/src/Manager.php new file mode 100644 index 0000000..215ec50 --- /dev/null +++ b/src/Manager.php @@ -0,0 +1,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; + } +} |
