summaryrefslogtreecommitdiff
path: root/src/Field.php
diff options
context:
space:
mode:
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;
+}