1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ngutech/lightning-interop project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NGUtech\Lightning\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
13
|
|
|
use Daikon\ValueObject\ValueObjectInterface; |
14
|
|
|
|
15
|
|
|
final class PaymentState implements MakeEmptyInterface, ValueObjectInterface |
16
|
|
|
{ |
17
|
|
|
public const PENDING = 'pending'; |
18
|
|
|
public const COMPLETED = 'completed'; |
19
|
|
|
public const FAILED = 'failed'; |
20
|
|
|
|
21
|
|
|
public const STATES = [ |
22
|
|
|
self::PENDING, |
23
|
|
|
self::COMPLETED, |
24
|
|
|
self::FAILED |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
private ?string $state; |
28
|
|
|
|
29
|
|
|
/** @param null|string $state */ |
30
|
|
|
public static function fromNative($state): self |
31
|
|
|
{ |
32
|
|
|
Assertion::nullOrString($state); |
33
|
|
|
return new self($state); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toNative(): ?string |
37
|
|
|
{ |
38
|
|
|
return $this->state; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @param self $comparator */ |
42
|
|
|
public function equals($comparator): bool |
43
|
|
|
{ |
44
|
|
|
Assertion::isInstanceOf($comparator, self::class); |
45
|
|
|
return $this->toNative() === $comparator->toNative(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function makeEmpty(): self |
49
|
|
|
{ |
50
|
|
|
return new self; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function isEmpty(): bool |
54
|
|
|
{ |
55
|
|
|
return $this->state === null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isPending(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->state === self::PENDING; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isFailed(): bool |
64
|
|
|
{ |
65
|
|
|
return $this->state === self::FAILED; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isCompleted(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->state === self::COMPLETED; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __toString(): string |
74
|
|
|
{ |
75
|
|
|
return (string)$this->state; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function __construct(?string $state = null) |
79
|
|
|
{ |
80
|
|
|
Assertion::nullOrinArray($state, self::STATES); |
81
|
|
|
$this->state = $state; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|