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_FAILED = 'failed'; |
25
|
|
|
|
26
|
|
|
private const STATE_PREMATURE = 'premature'; |
27
|
|
|
|
28
|
|
|
private const STATE_FUTURE = 'future'; |
29
|
|
|
|
30
|
|
|
private const STATE_CANCELED = 'cancelled'; |
31
|
|
|
|
32
|
|
|
private function __construct(protected string $state = self::STATE_NEW) |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getName(): string |
37
|
|
|
{ |
38
|
|
|
return $this->state; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isNew(): bool |
42
|
|
|
{ |
43
|
|
|
return $this->state === self::STATE_NEW; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function isNotActive(): bool |
47
|
|
|
{ |
48
|
|
|
return !$this->isNew(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function new(): self |
52
|
|
|
{ |
53
|
|
|
return new self(self::STATE_NEW); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function finished(): self |
57
|
|
|
{ |
58
|
|
|
return new self(self::STATE_FINISHED); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function failed(): self |
62
|
|
|
{ |
63
|
|
|
return new self(self::STATE_FAILED); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function premature(): self |
67
|
|
|
{ |
68
|
|
|
return new self(self::STATE_PREMATURE); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function future(): self |
72
|
|
|
{ |
73
|
|
|
return new self(self::STATE_FUTURE); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function canceled(): self |
77
|
|
|
{ |
78
|
|
|
return new self(self::STATE_CANCELED); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function fromString(string $name): self |
82
|
|
|
{ |
83
|
|
|
$allowedStates = [ |
84
|
|
|
self::STATE_NEW, |
85
|
|
|
self::STATE_FINISHED, |
86
|
|
|
self::STATE_FAILED, |
87
|
|
|
self::STATE_PREMATURE, |
88
|
|
|
self::STATE_FUTURE, |
89
|
|
|
self::STATE_CANCELED, |
90
|
|
|
]; |
91
|
|
|
foreach ($allowedStates as $state) { |
92
|
|
|
if ($state === $name) { |
93
|
|
|
return new self($state); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new \Exception("wrong action state '$name'"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function equals(ActionState $other): bool |
101
|
|
|
{ |
102
|
|
|
return $this->state === $other->getName(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|