1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance; |
4
|
|
|
|
5
|
|
|
use Dgame\Ensurance\Exception\EnsuranceException; |
6
|
|
|
use Dgame\Type\Type; |
7
|
|
|
use Dgame\Type\TypeFactory; |
8
|
|
|
use Exception; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait EnsuranceTrait |
13
|
|
|
* @package Dgame\Ensurance |
14
|
|
|
*/ |
15
|
|
|
trait EnsuranceTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
private $value; |
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $ensured = true; |
25
|
|
|
/** |
26
|
|
|
* @var Throwable|null |
27
|
|
|
*/ |
28
|
|
|
private $throwable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws Throwable |
32
|
|
|
*/ |
33
|
|
|
public function __destruct() |
34
|
|
|
{ |
35
|
|
|
if ($this->throwable !== null) { |
36
|
|
|
throw $this->throwable; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param callable $callback |
42
|
|
|
* |
43
|
|
|
* @return self |
44
|
|
|
*/ |
45
|
|
|
final public function is(callable $callback): self |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$result = (bool) $callback($this->value); |
49
|
|
|
} catch (Throwable $t) { |
50
|
|
|
$result = false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->ensure($result); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $type |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
* @throws Exception |
61
|
|
|
*/ |
62
|
|
|
final public function isTypeOf(string $type): self |
63
|
|
|
{ |
64
|
|
|
$type = Type::import($type); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $this->ensure(TypeFactory::expression($this->value)->isSame($type)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $value |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
final public function then($value) |
75
|
|
|
{ |
76
|
|
|
return $this->disregardThrowable()->isEnsured() ? $value : $this->value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $value |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
final public function else($value) |
85
|
|
|
{ |
86
|
|
|
return $this->disregardThrowable()->isEnsured() ? $this->value : $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $value |
91
|
|
|
* |
92
|
|
|
* @return Either |
93
|
|
|
*/ |
94
|
|
|
final public function either($value): Either |
95
|
|
|
{ |
96
|
|
|
return new Either($value, $this->disregardThrowable()->isEnsured()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param mixed $default |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
final public function get($default = null) |
105
|
|
|
{ |
106
|
|
|
return $this->value ?? $default; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param bool $condition |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
|
|
final protected function ensure(bool $condition): self |
115
|
|
|
{ |
116
|
|
|
$this->ensured = $condition; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
final public function isEnsured(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->ensured; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
final public function disregardThrowable(): self |
133
|
|
|
{ |
134
|
|
|
$this->throwable = null; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Throwable|null |
141
|
|
|
*/ |
142
|
|
|
final public function releaseThrowable(): ?Throwable |
143
|
|
|
{ |
144
|
|
|
try { |
145
|
|
|
return $this->throwable; |
146
|
|
|
} finally { |
147
|
|
|
$this->disregardThrowable(); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param EnsuranceInterface $ensurance |
153
|
|
|
* |
154
|
|
|
* @return self |
155
|
|
|
*/ |
156
|
|
|
final public function transferEnsurance(EnsuranceInterface $ensurance): self |
157
|
|
|
{ |
158
|
|
|
$this->value = $ensurance->get(); |
159
|
|
|
$this->ensure($ensurance->isEnsured()); |
160
|
|
|
|
161
|
|
|
$throwable = $ensurance->releaseThrowable(); |
162
|
|
|
if ($throwable !== null) { |
163
|
|
|
$this->orThrowWith($throwable); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $message |
171
|
|
|
* @param mixed ...$args |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
|
|
|
|
175
|
|
|
final public function orThrow(string $message, ...$args): self |
176
|
|
|
{ |
177
|
|
|
if (!$this->isEnsured()) { |
178
|
|
|
$this->orThrowWith(new EnsuranceException(format($message, ...$args), $this->throwable)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
final public function hasThrowable(): bool |
188
|
|
|
{ |
189
|
|
|
return $this->throwable !== null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param Throwable $throwable |
194
|
|
|
* |
195
|
|
|
* @return self |
196
|
|
|
* @deprecated Use "orThrowWith" instead |
197
|
|
|
* |
198
|
|
|
*/ |
199
|
|
|
final public function setThrowable(Throwable $throwable): self |
200
|
|
|
{ |
201
|
|
|
return $this->orThrowWith($throwable); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param Throwable $throwable |
206
|
|
|
* |
207
|
|
|
* @return self |
208
|
|
|
*/ |
209
|
|
|
final public function orThrowWith(Throwable $throwable): self |
210
|
|
|
{ |
211
|
|
|
if (!$this->isEnsured()) { |
212
|
|
|
$this->throwable = $throwable; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return Throwable|null |
220
|
|
|
*/ |
221
|
|
|
final public function getThrowable(): ?Throwable |
222
|
|
|
{ |
223
|
|
|
return $this->throwable; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|