summaryrefslogtreecommitdiff
path: root/src/Field.php
blob: 9e11bad17af19b909c537e050cccf4f7b3681b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}