|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance\Enforcement; |
|
4
|
|
|
|
|
5
|
|
|
use AssertionError; |
|
6
|
|
|
use Dgame\Ensurance\Exception\EnsuranceException; |
|
7
|
|
|
use Throwable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Enforcement |
|
11
|
|
|
* @package Dgame\Ensurance\Enforcement |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Enforcement |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
private $condition; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var null|Throwable |
|
21
|
|
|
*/ |
|
22
|
|
|
private $throwable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Enforcement constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param bool $condition |
|
28
|
|
|
* @param string|null $message |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(bool $condition, string $message = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->condition = $condition; |
|
33
|
|
|
if ($message !== null) { |
|
34
|
|
|
$this->setThrowMessage($message); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws Throwable |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __destruct() |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$this->isFulfilled()) { |
|
44
|
|
|
throw $this->getThrowable(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Enforce approvement of the Enforcement |
|
50
|
|
|
*/ |
|
51
|
|
|
final public function approve() |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$this->condition = true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
final public function isFulfilled(): bool |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
return $this->condition; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
final public function hasThrowable(): bool |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
return $this->throwable !== null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Throwable |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getThrowable(): Throwable |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->throwable ?? $this->emplaceThrowable('Assertion failed'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
* |
|
83
|
|
|
* @return Throwable |
|
84
|
|
|
*/ |
|
85
|
|
|
private function emplaceThrowable(string $message): Throwable |
|
86
|
|
|
{ |
|
87
|
|
|
return new EnsuranceException($message); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string|Throwable $throwable |
|
92
|
|
|
* @param array ...$args |
|
93
|
|
|
*/ |
|
|
|
|
|
|
94
|
|
|
final public function orThrow($throwable, ...$args) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
if ($throwable instanceof Throwable) { |
|
97
|
|
|
$this->setThrowable($throwable); |
|
98
|
|
|
} else { |
|
99
|
|
|
$this->setThrowMessage((string) $throwable, ...$args); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $message |
|
105
|
|
|
* @param array ...$args |
|
106
|
|
|
*/ |
|
|
|
|
|
|
107
|
|
|
final public function setThrowMessage(string $message, ...$args) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if (!empty($args)) { |
|
110
|
|
|
$args = array_map(function ($arg) { |
|
|
|
|
|
|
111
|
|
|
return !is_string($arg) ? var_export($arg, true) : $arg; |
|
112
|
|
|
}, $args); |
|
113
|
|
|
$message = sprintf($message, ...$args); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$throwable = $this->emplaceThrowable($message); |
|
117
|
|
|
|
|
118
|
|
|
$this->setThrowable($throwable); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Throwable $throwable |
|
123
|
|
|
*/ |
|
124
|
|
|
final public function setThrowable(Throwable $throwable) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
$this->throwable = $throwable; |
|
127
|
|
|
} |
|
128
|
|
|
} |