summaryrefslogtreecommitdiff
path: root/src/Field.php
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-01-05 16:26:11 +0800
committerZhineng Li <[email protected]>2026-01-05 16:26:11 +0800
commita49763dd739c3c68c4a8322896d594e926ac8e6b (patch)
treeab96cf55ccf828ec3c61d7cf5c440cc8fe837a9b /src/Field.php
first commit
Diffstat (limited to 'src/Field.php')
-rw-r--r--src/Field.php58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/Field.php b/src/Field.php
new file mode 100644
index 0000000..9e11bad
--- /dev/null
+++ b/src/Field.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Snowflake;
+
+abstract class Field implements Component
+{
+ /**
+ * The bit offset.
+ */
+ private int $offset = 0;
+
+ public function __construct(
+ public readonly string $name,
+ public readonly int $bits,
+ ) {
+ if ($bits < 1 || $bits > 63) {
+ throw new \InvalidArgumentException('Bits must be between 1 and 63.');
+ }
+ }
+
+ /**
+ * The bit size.
+ */
+ public function bits(): int
+ {
+ return $this->bits;
+ }
+
+ /**
+ * The maximum value based on the number of bits.
+ */
+ public function maxValue(): int
+ {
+ return (1 << $this->bits) - 1;
+ }
+
+ /**
+ * Set the bit offset.
+ */
+ public function setOffset(int $offset): static
+ {
+ $this->offset = $offset;
+
+ return $this;
+ }
+
+ /**
+ * The bit offset.
+ */
+ public function offset(): int
+ {
+ return $this->offset;
+ }
+
+ abstract public function value(): int;
+}