|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
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
|
11 |
|
public function __construct($value = null, array $message = [], $predicate = null) |
|
24
|
|
|
{ |
|
25
|
11 |
|
$this->result = $value; |
|
26
|
11 |
|
$this->message = $message; |
|
27
|
11 |
|
$this->predicate = $predicate; |
|
28
|
11 |
|
} |
|
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 = []) |
|
46
|
|
|
{ |
|
47
|
2 |
|
if (!is_null($this->result)) { |
|
48
|
1 |
|
return $this->result; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
abort($code, $message, $headers); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
public function getOrSend($callable) |
|
55
|
|
|
{ |
|
56
|
6 |
|
if (!is_null($this->result)) { |
|
57
|
1 |
|
return $this->result; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
if (is_callable($callable)) { |
|
61
|
3 |
|
$callable = call_user_func_array($callable, $this->message); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
if (is_a($callable, Response::class)) { |
|
65
|
3 |
|
$response = $callable; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
if (isset($response)) { |
|
69
|
3 |
|
throw new HttpResponseException($response); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
throw new \InvalidArgumentException('You must provide a valid http response or a callable.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function getOrThrow($exception, ...$parameters) |
|
76
|
|
|
{ |
|
77
|
|
|
|
|
78
|
3 |
|
if (!is_null($this->result)) { |
|
79
|
1 |
|
return $this->result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
throw_if(true, $exception, ...$parameters); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function onValue(callable $callable) |
|
86
|
|
|
{ |
|
87
|
|
|
if (! is_null($this->result)) { |
|
88
|
|
|
return call_user_func_array($callable, [$this->result]); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private function getPredicate() |
|
93
|
|
|
{ |
|
94
|
1 |
|
if (is_callable($this->predicate)) { |
|
95
|
1 |
|
$p = $this->predicate; |
|
96
|
|
|
} else { |
|
97
|
|
|
$p = function ($r) { |
|
98
|
1 |
|
return is_null($r); |
|
99
|
1 |
|
}; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
return $p; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|