1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/event-sourcing project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\EventSourcing\EventStore\Stream; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\FromNativeInterface; |
12
|
|
|
use Daikon\Interop\ToNativeInterface; |
13
|
|
|
|
14
|
|
|
final class Sequence implements FromNativeInterface, ToNativeInterface |
15
|
|
|
{ |
16
|
|
|
private const INITIAL = 1; |
17
|
|
|
|
18
|
|
|
private const NONE = 0; |
19
|
|
|
|
20
|
|
|
private int $seqNumber; |
21
|
|
|
|
22
|
|
|
/** @param int $seqNumber */ |
23
|
1 |
|
public static function fromNative($seqNumber): self |
24
|
|
|
{ |
25
|
1 |
|
return new self($seqNumber); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function makeInitial(): self |
29
|
|
|
{ |
30
|
|
|
return new self(self::NONE); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function toNative(): int |
34
|
|
|
{ |
35
|
1 |
|
return $this->seqNumber; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function increment(): self |
39
|
|
|
{ |
40
|
|
|
$copy = clone $this; |
41
|
|
|
$copy->seqNumber++; |
42
|
|
|
return $copy; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function decrement(): self |
46
|
|
|
{ |
47
|
|
|
$copy = clone $this; |
48
|
|
|
$copy->seqNumber--; |
49
|
|
|
return $copy; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function equals(Sequence $seqNumber): bool |
53
|
|
|
{ |
54
|
|
|
return $this->seqNumber === $seqNumber->toNative(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isInitial(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->seqNumber === self::INITIAL; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isWithinRange(Sequence $from, Sequence $to): bool |
63
|
|
|
{ |
64
|
|
|
return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function isGreaterThanOrEqual(Sequence $seqNumber): bool |
68
|
|
|
{ |
69
|
|
|
return $this->seqNumber >= $seqNumber->toNative(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function isLessThanOrEqual(Sequence $seqNumber): bool |
73
|
|
|
{ |
74
|
|
|
return $this->seqNumber <= $seqNumber->toNative(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function __toString(): string |
78
|
|
|
{ |
79
|
|
|
return (string)$this->seqNumber; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
private function __construct(int $seqNumber) |
83
|
|
|
{ |
84
|
1 |
|
$this->seqNumber = $seqNumber; |
85
|
1 |
|
} |
86
|
|
|
} |
87
|
|
|
|