summaryrefslogtreecommitdiff
path: root/src/Sequence.php
blob: d799e29a0eba46b791006038a5e2adf5e7f92616 (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
<?php

declare(strict_types=1);

namespace Zhineng\Snowflake;

final class Sequence extends Field
{
    use Makable;

    /**
     * The current value.
     */
    private int $value = 0;

    public function value(): int
    {
        return $this->next();
    }

    public function next(): int
    {
        $current = $this->value++;

        if ($current > $this->maxValue()) {
            throw new \OverflowException(sprintf(
                'Sequence "%s" exceeded its maximum value of %d.',
                $this->name,
                $this->maxValue()
            ));
        }

        return $current;
    }

    public function reset(): self
    {
        $this->value = 0;

        return $this;
    }
}