-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathActionState.php
More file actions
104 lines (84 loc) · 2.17 KB
/
Copy pathActionState.php
File metadata and controls
104 lines (84 loc) · 2.17 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\action;
/**
* Action State.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class ActionState
{
private const STATE_NEW = 'new';
private const STATE_FINISHED = 'finished';
private const STATE_FAILED = 'failed';
private const STATE_PREMATURE = 'premature';
private const STATE_FUTURE = 'future';
private const STATE_CANCELED = 'cancelled';
private function __construct(protected string $state = self::STATE_NEW)
{
}
public function getName(): string
{
return $this->state;
}
public function isNew(): bool
{
return $this->state === self::STATE_NEW;
}
public function isNotActive(): bool
{
return !$this->isNew();
}
public static function new(): self
{
return new self(self::STATE_NEW);
}
public static function finished(): self
{
return new self(self::STATE_FINISHED);
}
public static function failed(): self
{
return new self(self::STATE_FAILED);
}
public static function premature(): self
{
return new self(self::STATE_PREMATURE);
}
public static function future(): self
{
return new self(self::STATE_FUTURE);
}
public static function canceled(): self
{
return new self(self::STATE_CANCELED);
}
public static function fromString(string $name): self
{
$allowedStates = [
self::STATE_NEW,
self::STATE_FINISHED,
self::STATE_FAILED,
self::STATE_PREMATURE,
self::STATE_FUTURE,
self::STATE_CANCELED,
];
foreach ($allowedStates as $state) {
if ($state === $name) {
return new self($state);
}
}
throw new \Exception("wrong action state '$name'");
}
public function equals(ActionState $other): bool
{
return $this->state === $other->getName();
}
}