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