1 | <?php |
||
14 | trait EnsuranceTrait |
||
15 | { |
||
16 | /** |
||
17 | * @var mixed |
||
18 | */ |
||
19 | private $value; |
||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $ensured = true; |
||
24 | /** |
||
25 | * @var Throwable |
||
26 | */ |
||
27 | private $throwable; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | */ |
||
32 | public function __destruct() |
||
38 | |||
39 | /** |
||
40 | * @param callable $callback |
||
41 | * |
||
42 | * @return self |
||
43 | */ |
||
44 | final public function is(callable $callback): self |
||
54 | |||
55 | /** |
||
56 | * @param string $type |
||
57 | * |
||
58 | * @return EnsuranceTrait |
||
|
|||
59 | * @throws \Exception |
||
60 | */ |
||
61 | final public function isTypeOf(string $type): self |
||
67 | |||
68 | /** |
||
69 | * @param $value |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | final public function then($value) |
||
77 | |||
78 | /** |
||
79 | * @param $value |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | final public function else($value) |
||
87 | |||
88 | /** |
||
89 | * @param $value |
||
90 | * |
||
91 | * @return Either |
||
92 | */ |
||
93 | final public function either($value): Either |
||
97 | |||
98 | /** |
||
99 | * @param mixed $default |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | final public function get($default = null) |
||
107 | |||
108 | /** |
||
109 | * @param bool $condition |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | final protected function ensure(bool $condition): self |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | final public function isEnsured(): bool |
||
127 | |||
128 | /** |
||
129 | * @return self |
||
130 | */ |
||
131 | final public function disregardThrowable(): self |
||
137 | |||
138 | /** |
||
139 | * @return Throwable |
||
140 | */ |
||
141 | final public function releaseThrowable(): Throwable |
||
149 | |||
150 | /** |
||
151 | * @param EnsuranceInterface $ensurance |
||
152 | * |
||
153 | * @return self |
||
154 | */ |
||
155 | final public function transferEnsurance(EnsuranceInterface $ensurance): self |
||
165 | |||
166 | /** |
||
167 | * @param string $message |
||
168 | * @param mixed ...$args |
||
169 | * |
||
170 | * @return self |
||
171 | */ |
||
172 | final public function orThrow(string $message, ...$args): self |
||
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | final public function hasThrowable(): bool |
||
188 | |||
189 | /** |
||
190 | * @param Throwable $throwable |
||
191 | * |
||
192 | * @deprecated Use "orThrowWith" instead |
||
193 | * |
||
194 | * @return EnsuranceTrait |
||
195 | */ |
||
196 | final public function setThrowable(Throwable $throwable): self |
||
200 | |||
201 | /** |
||
202 | * @param Throwable $throwable |
||
203 | * |
||
204 | * @return self |
||
205 | */ |
||
206 | final public function orThrowWith(Throwable $throwable): self |
||
214 | |||
215 | /** |
||
216 | * @return Throwable |
||
217 | */ |
||
218 | final public function getThrowable(): Throwable |
||
222 | } |
||
223 |
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.