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