summaryrefslogtreecommitdiff
path: root/src/Constant.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Constant.php')
-rw-r--r--src/Constant.php36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Constant.php b/src/Constant.php
new file mode 100644
index 0000000..6cd63a7
--- /dev/null
+++ b/src/Constant.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Snowflake;
+
+final class Constant extends Field
+{
+ use Makable;
+
+ public function __construct(
+ string $name,
+ int $bits,
+ public readonly int $value = 0
+ ) {
+ parent::__construct($name, $bits);
+
+ if ($this->value < 0) {
+ throw new \InvalidArgumentException('Field value must be non-negative.');
+ }
+
+ if ($this->value > $this->maxValue()) {
+ throw new \InvalidArgumentException(sprintf(
+ 'Field value %d exceeds maximum %d for %d bits.',
+ $this->value,
+ $this->maxValue(),
+ $this->bits
+ ));
+ }
+ }
+
+ public function value(): int
+ {
+ return $this->value;
+ }
+}