1 | <?php |
||
8 | class Nullable |
||
9 | { |
||
10 | private $result; |
||
11 | |||
12 | private $predicate = null; |
||
13 | |||
14 | private $message = []; |
||
15 | |||
16 | /** |
||
17 | * Nullable constructor. |
||
18 | * |
||
19 | * @param mixed $value |
||
20 | * @param array $message |
||
21 | * @param callable $predicate |
||
22 | */ |
||
23 | 13 | public function __construct($value = null, array $message = [], $predicate = null) |
|
29 | |||
30 | 1 | public function getOr($default) |
|
31 | { |
||
32 | 1 | $p = $this->getPredicate(); |
|
33 | |||
34 | 1 | if (!$p($this->result)) { |
|
35 | 1 | return $this->result; |
|
36 | } |
||
37 | |||
38 | 1 | if (is_callable($default)) { |
|
39 | 1 | return call_user_func_array($default, $this->message); |
|
40 | } |
||
41 | |||
42 | 1 | return $default; |
|
43 | } |
||
44 | |||
45 | 2 | public function getOrAbort($code, $message = '', array $headers = []) |
|
53 | |||
54 | 6 | public function getOrSend($callable) |
|
74 | |||
75 | 3 | public function getOrThrow($exception, ...$parameters) |
|
83 | |||
84 | 2 | public function onValue(callable $callable) |
|
85 | { |
||
86 | 2 | if (! is_null($this->result)) { |
|
87 | 1 | return call_user_func_array($callable, [$this->result]); |
|
88 | } |
||
89 | 1 | } |
|
90 | |||
91 | 1 | private function getPredicate() |
|
101 | } |
||
102 |