summaryrefslogtreecommitdiff
path: root/src/Constant.php
blob: 6cd63a7a7cd26d70504500e9be4984f6ba2d72f4 (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
<?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;
    }
}