/
ConditionalWriteStatus.php
93 lines (74 loc) · 2.04 KB
/
ConditionalWriteStatus.php
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* This file is part of prooph/event-store.
* (c) 2014-2019 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStore;
use Prooph\EventStore\Exception\InvalidArgumentException;
class ConditionalWriteStatus
{
public const OPTIONS = [
'Succeeded' => 0,
'VersionMismatch' => 1,
'StreamDeleted' => 2,
];
public const SUCCEEDED = 0;
public const VERSION_MISMATCH = 1;
public const STREAM_DELETED = 2;
private $name;
private $value;
private function __construct(string $name)
{
$this->name = $name;
$this->value = self::OPTIONS[$name];
}
public static function succeeded(): self
{
return new self('Succeeded');
}
public static function versionMismatch(): self
{
return new self('VersionMismatch');
}
public static function streamDeleted(): self
{
return new self('StreamDeleted');
}
public static function byName(string $value): self
{
if (! isset(self::OPTIONS[$value])) {
throw new InvalidArgumentException('Unknown enum name given');
}
return self::{$value}();
}
public static function byValue($value): self
{
foreach (self::OPTIONS as $name => $v) {
if ($v === $value) {
return self::{$name}();
}
}
throw new InvalidArgumentException('Unknown enum value given');
}
public function equals(ConditionalWriteStatus $other): bool
{
return \get_class($this) === \get_class($other) && $this->name === $other->name;
}
public function name(): string
{
return $this->name;
}
public function value()
{
return $this->value;
}
public function __toString(): string
{
return $this->name;
}
}