1 | <?php |
||
13 | trait EnsuranceTrait |
||
14 | { |
||
15 | /** |
||
16 | * @var mixed |
||
17 | */ |
||
18 | private $value; |
||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | private $ensured = true; |
||
23 | /** |
||
24 | * @var Throwable |
||
25 | */ |
||
26 | private $throwable; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | public function __destruct() |
||
37 | |||
38 | /** |
||
39 | * @param callable $callback |
||
40 | * |
||
41 | * @return self |
||
42 | */ |
||
43 | final public function is(callable $callback): self |
||
53 | |||
54 | /** |
||
55 | * @param string $type |
||
56 | * |
||
57 | * @return EnsuranceTrait |
||
|
|||
58 | * @throws \Exception |
||
59 | */ |
||
60 | final public function isTypeOf(string $type): self |
||
66 | |||
67 | /** |
||
68 | * @param $value |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | final public function then($value) |
||
76 | |||
77 | /** |
||
78 | * @param $value |
||
79 | * |
||
80 | * @return mixed |
||
81 | */ |
||
82 | final public function else($value) |
||
86 | |||
87 | /** |
||
88 | * @param $value |
||
89 | * |
||
90 | * @return Either |
||
91 | */ |
||
92 | final public function either($value): Either |
||
96 | |||
97 | /** |
||
98 | * @param mixed $default |
||
99 | * |
||
100 | * @return mixed |
||
101 | */ |
||
102 | final public function get($default = null) |
||
106 | |||
107 | /** |
||
108 | * @param bool $condition |
||
109 | * |
||
110 | * @return self |
||
111 | */ |
||
112 | final protected function ensure(bool $condition): self |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | final public function isEnsured(): bool |
||
126 | |||
127 | /** |
||
128 | * @return self |
||
129 | */ |
||
130 | final public function disregardThrowable(): self |
||
136 | |||
137 | /** |
||
138 | * @return Throwable |
||
139 | */ |
||
140 | final public function releaseThrowable(): Throwable |
||
148 | |||
149 | /** |
||
150 | * @param EnsuranceInterface $ensurance |
||
151 | * |
||
152 | * @return self |
||
153 | */ |
||
154 | final public function transferEnsurance(EnsuranceInterface $ensurance): self |
||
164 | |||
165 | /** |
||
166 | * @param string $message |
||
167 | * @param mixed ...$args |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | final public function orThrow(string $message, ...$args): self |
||
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | final public function hasThrowable(): bool |
||
187 | |||
188 | /** |
||
189 | * @param Throwable $throwable |
||
190 | * |
||
191 | * @return self |
||
192 | */ |
||
193 | final public function setThrowable(Throwable $throwable): self |
||
201 | |||
202 | /** |
||
203 | * @return Throwable |
||
204 | */ |
||
205 | final public function getThrowable(): Throwable |
||
209 | } |
||
210 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.