-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecision.php
More file actions
79 lines (61 loc) · 1.73 KB
/
Decision.php
File metadata and controls
79 lines (61 loc) · 1.73 KB
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
<?php
namespace Slutsky\EnumExample;
use InvalidArgumentException;
class Decision
{
public const AGREE = 'agree';
public const DISAGREE = 'disagree';
public const HOLD = 'hold';
private string $value;
private function __construct(string $value)
{
$this->value = $value;
}
private function __clone() { }
public function __toString(): string
{
return $this->value;
}
private static $agreeInstance = null;
private static $disagreeInstance = null;
private static $holdInstance = null;
public static function agree(): self
{
if (null === self::$agreeInstance) {
self::$agreeInstance = new self(self::AGREE);
}
return self::$agreeInstance;
}
public static function disagree(): self
{
if (null === self::$disagreeInstance) {
self::$disagreeInstance = new self(self::DISAGREE);
}
return self::$disagreeInstance;
}
public static function hold(): self
{
if (null === self::$holdInstance) {
self::$holdInstance = new self(self::HOLD);
}
return self::$holdInstance;
}
public static function from($value): self
{
switch ($value) {
case self::AGREE:
return self::agree();
case self::DISAGREE:
return self::disagree();
case self::HOLD:
return self::hold();
default:
throw new InvalidArgumentException(sprintf(
"Wrong decision value. Awaited '%s', '%s' or '%s'.",
self::AGREE,
self::DISAGREE,
self::HOLD
));
}
}
}