1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/cqrs 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
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\EventStore; |
12
|
|
|
|
13
|
|
|
use Assert\Assertion; |
14
|
|
|
use Daikon\Entity\ValueObject\ValueObjectInterface; |
15
|
|
|
|
16
|
|
|
final class StreamRevision implements ValueObjectInterface |
17
|
|
|
{ |
18
|
|
|
private const INITIAL = 1; |
19
|
|
|
|
20
|
|
|
private const NONE = 0; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
private $revision; |
24
|
|
|
|
25
|
1 |
|
public static function fromNative($revision): ValueObjectInterface |
26
|
|
|
{ |
27
|
1 |
|
return new self($revision); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function makeEmpty(): ValueObjectInterface |
31
|
|
|
{ |
32
|
|
|
return new static(self::NONE); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function toNative(): int |
36
|
|
|
{ |
37
|
1 |
|
return $this->revision; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function increment(): StreamRevision |
41
|
|
|
{ |
42
|
|
|
$copy = clone $this; |
43
|
|
|
$copy->revision++; |
44
|
|
|
return $copy; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function decrement(): StreamRevision |
48
|
|
|
{ |
49
|
|
|
$copy = clone $this; |
50
|
|
|
$copy->revision--; |
51
|
|
|
return $copy; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function equals(ValueObjectInterface $otherValue): bool |
55
|
|
|
{ |
56
|
|
|
Assertion::isInstanceOf($otherValue, static::class); |
57
|
|
|
return $otherValue->toNative() === $this->revision; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isEmpty(): bool |
61
|
|
|
{ |
62
|
|
|
return $this->revision === self::NONE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isInitial(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->revision === self::INITIAL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function isWithinRange(StreamRevision $from, StreamRevision $to) |
71
|
|
|
{ |
72
|
|
|
return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function isGreaterThanOrEqual(StreamRevision $revision) |
76
|
|
|
{ |
77
|
|
|
return $this->revision >= $revision->toNative(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isLessThanOrEqual(StreamRevision $revision) |
81
|
|
|
{ |
82
|
|
|
return $this->revision <= $revision->toNative(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function __toString(): string |
86
|
|
|
{ |
87
|
|
|
return (string)$this->revision; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
private function __construct(int $revision) |
91
|
|
|
{ |
92
|
1 |
|
$this->revision = $revision; |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|