1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\action; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Action State. |
15
|
|
|
* |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ActionState |
19
|
|
|
{ |
20
|
|
|
private const STATE_NEW = 'new'; |
21
|
|
|
|
22
|
|
|
private const STATE_FINISHED = 'finished'; |
23
|
|
|
|
24
|
|
|
private const STATE_PREMATURE = 'premature'; |
25
|
|
|
|
26
|
|
|
private const STATE_FUTURE = 'future'; |
27
|
|
|
|
28
|
|
|
private const STATE_CANCELED = 'canceled'; |
29
|
|
|
|
30
|
|
|
private function __construct(protected string $state = self::STATE_NEW) |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getName(): string |
35
|
|
|
{ |
36
|
|
|
return $this->state; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function isNew(): bool |
40
|
|
|
{ |
41
|
|
|
return $this->state === self::STATE_NEW; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function isNotActive(): bool |
45
|
|
|
{ |
46
|
|
|
return !$this->isNew(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function new(): self |
50
|
|
|
{ |
51
|
|
|
return new self(self::STATE_NEW); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function finished(): self |
55
|
|
|
{ |
56
|
|
|
return new self(self::STATE_FINISHED); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function premature(): self |
60
|
|
|
{ |
61
|
|
|
return new self(self::STATE_PREMATURE); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function future(): self |
65
|
|
|
{ |
66
|
|
|
return new self(self::STATE_FUTURE); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function canceled(): self |
70
|
|
|
{ |
71
|
|
|
return new self(self::STATE_CANCELED); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function fromString(string $name): self |
75
|
|
|
{ |
76
|
|
|
$allowedStates = [ |
77
|
|
|
self::STATE_NEW, |
78
|
|
|
self::STATE_FINISHED, |
79
|
|
|
self::STATE_PREMATURE, |
80
|
|
|
self::STATE_FUTURE, |
81
|
|
|
self::STATE_CANCELED, |
82
|
|
|
]; |
83
|
|
|
foreach ($allowedStates as $state) { |
84
|
|
|
if ($state === $name) { |
85
|
|
|
return new self($state); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new \Exception("wrong action state '$name'"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function equals(ActionState $other): bool |
93
|
|
|
{ |
94
|
|
|
return $this->state === $other->getName(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|