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