1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance; |
4
|
|
|
|
5
|
|
|
use Dgame\Ensurance\Exception\EnsuranceException; |
6
|
|
|
use Throwable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait EnsuranceTrait |
10
|
|
|
* @package Dgame\Ensurance |
11
|
|
|
*/ |
12
|
|
|
trait EnsuranceTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
private $value; |
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $ensured = true; |
22
|
|
|
/** |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
private $throwable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function __destruct() |
31
|
|
|
{ |
32
|
|
|
if ($this->hasThrowable()) { |
33
|
|
|
throw $this->throwable; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $value |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
final public function then($value) |
43
|
|
|
{ |
44
|
|
|
return $this->disregardThrowable()->isEnsured() ? $value : $this->value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $value |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
final public function else($value) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return $this->disregardThrowable()->isEnsured() ? $this->value : $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $value |
59
|
|
|
* |
60
|
|
|
* @return Either |
61
|
|
|
*/ |
62
|
|
|
final public function either($value): Either |
63
|
|
|
{ |
64
|
|
|
return new Either($value, $this->disregardThrowable()->isEnsured()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param null $default |
69
|
|
|
* |
70
|
|
|
* @return null |
71
|
|
|
*/ |
72
|
|
|
final public function get($default = null) |
73
|
|
|
{ |
74
|
|
|
return $this->value ?? $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param bool $condition |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
final protected function ensure(bool $condition): self |
83
|
|
|
{ |
84
|
|
|
$this->ensured = $condition; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
final public function isEnsured(): bool |
93
|
|
|
{ |
94
|
|
|
return $this->ensured; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
|
|
final public function disregardThrowable(): self |
101
|
|
|
{ |
102
|
|
|
$this->throwable = null; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Throwable |
109
|
|
|
*/ |
110
|
|
|
final public function releaseThrowable(): Throwable |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
return $this->throwable; |
114
|
|
|
} finally { |
115
|
|
|
$this->disregardThrowable(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param EnsuranceInterface $ensurance |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
final public function transferEnsurance(EnsuranceInterface $ensurance): self |
125
|
|
|
{ |
126
|
|
|
$this->value = $ensurance->get(); |
127
|
|
|
$this->ensure($ensurance->isEnsured()); |
128
|
|
|
if ($ensurance->hasThrowable()) { |
129
|
|
|
$this->setThrowable($ensurance->releaseThrowable()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $message |
137
|
|
|
* @param mixed ...$args |
138
|
|
|
* |
139
|
|
|
* @return self |
140
|
|
|
*/ |
|
|
|
|
141
|
|
|
final public function orThrow(string $message, ...$args): self |
142
|
|
|
{ |
143
|
|
|
if (!$this->isEnsured()) { |
144
|
|
|
$this->setThrowable(new EnsuranceException(format($message, ...$args), $this->throwable)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
final public function hasThrowable(): bool |
154
|
|
|
{ |
155
|
|
|
return $this->throwable !== null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param Throwable $throwable |
160
|
|
|
* |
161
|
|
|
* @return self |
162
|
|
|
*/ |
163
|
|
|
final public function setThrowable(Throwable $throwable): self |
164
|
|
|
{ |
165
|
|
|
if (!$this->isEnsured()) { |
166
|
|
|
$this->throwable = $throwable; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return Throwable |
174
|
|
|
*/ |
175
|
|
|
final public function getThrowable(): Throwable |
176
|
|
|
{ |
177
|
|
|
return $this->throwable; |
178
|
|
|
} |
179
|
|
|
} |