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 const STATE_EXPIRED = 'expired'; |
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 isFinished(): 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
|
|
|
/** |
57
|
|
|
* @deprecated use ActionState::expired() |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
|
|
public static function finished(): self |
61
|
|
|
{ |
62
|
|
|
return new self(self::STATE_FINISHED); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function premature(): self |
66
|
|
|
{ |
67
|
|
|
return new self(self::STATE_PREMATURE); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function future(): self |
71
|
|
|
{ |
72
|
|
|
return new self(self::STATE_FUTURE); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function canceled(): self |
76
|
|
|
{ |
77
|
|
|
return new self(self::STATE_CANCELED); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function expired(): self |
81
|
|
|
{ |
82
|
|
|
return new self(self::STATE_EXPIRED); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function fromString(string $name): self |
86
|
|
|
{ |
87
|
|
|
$allowedStates = [ |
88
|
|
|
self::STATE_NEW, |
89
|
|
|
self::STATE_FINISHED, |
90
|
|
|
self::STATE_PREMATURE, |
91
|
|
|
self::STATE_FUTURE, |
92
|
|
|
self::STATE_CANCELED, |
93
|
|
|
self::STATE_EXPIRED, |
94
|
|
|
]; |
95
|
|
|
foreach ($allowedStates as $state) { |
96
|
|
|
if ($state === $name) { |
97
|
|
|
return new self($state); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
throw new \Exception("wrong action state '$name'"); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|