diff options
Diffstat (limited to 'src/Sequence.php')
| -rw-r--r-- | src/Sequence.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Sequence.php b/src/Sequence.php new file mode 100644 index 0000000..d799e29 --- /dev/null +++ b/src/Sequence.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +namespace Zhineng\Snowflake; + +final class Sequence extends Field +{ + use Makable; + + /** + * The current value. + */ + private int $value = 0; + + public function value(): int + { + return $this->next(); + } + + public function next(): int + { + $current = $this->value++; + + if ($current > $this->maxValue()) { + throw new \OverflowException(sprintf( + 'Sequence "%s" exceeded its maximum value of %d.', + $this->name, + $this->maxValue() + )); + } + + return $current; + } + + public function reset(): self + { + $this->value = 0; + + return $this; + } +} |
