|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan\Reactions; |
|
4
|
|
|
|
|
5
|
|
|
use Imanghafoori\HeyMan\Chain; |
|
6
|
|
|
use Imanghafoori\HeyMan\HeyManSwitcher; |
|
7
|
|
|
|
|
8
|
|
|
class ResponderFactory |
|
9
|
|
|
{ |
|
10
|
|
|
private $chain; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* ResponderFactory constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Chain $chain |
|
16
|
|
|
*/ |
|
17
|
78 |
|
public function __construct(Chain $chain) |
|
18
|
|
|
{ |
|
19
|
78 |
|
$this->chain = $chain; |
|
20
|
78 |
|
} |
|
21
|
|
|
|
|
22
|
78 |
|
public function make() |
|
23
|
|
|
{ |
|
24
|
78 |
|
$m = $this->chain->methodName; |
|
25
|
78 |
|
$parameters = $this->chain->data; |
|
26
|
|
|
|
|
27
|
78 |
|
return $this->$m($parameters); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
public function abort($abort) |
|
31
|
|
|
{ |
|
32
|
|
|
return function () use ($abort) { |
|
33
|
2 |
|
abort(...$abort[0]); |
|
|
|
|
|
|
34
|
2 |
|
}; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
6 |
|
public function nothing() |
|
38
|
|
|
{ |
|
39
|
|
|
return function () { |
|
40
|
6 |
|
}; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $e |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Closure |
|
47
|
|
|
*/ |
|
48
|
68 |
|
public function exception($e): \Closure |
|
49
|
|
|
{ |
|
50
|
|
|
return function () use ($e) { |
|
51
|
39 |
|
$exClass = $e[0]['class']; |
|
52
|
|
|
|
|
53
|
39 |
|
throw new $exClass($e[0]['message']); |
|
54
|
68 |
|
}; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $resp |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Closure |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function response($resp): \Closure |
|
63
|
|
|
{ |
|
64
|
|
|
return function () use ($resp) { |
|
65
|
1 |
|
$respObj = response(); |
|
66
|
1 |
|
foreach ($resp as $call) { |
|
67
|
1 |
|
list($method, $args) = $call; |
|
68
|
1 |
|
$respObj = $respObj->{$method}(...$args); |
|
69
|
|
|
} |
|
70
|
1 |
|
respondWith($respObj); |
|
71
|
1 |
|
}; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
public function redirect($resp): \Closure |
|
75
|
|
|
{ |
|
76
|
|
|
return function () use ($resp) { |
|
77
|
4 |
|
$respObj = redirect(); |
|
78
|
4 |
|
foreach ($resp as $call) { |
|
79
|
4 |
|
list($method, $args) = $call; |
|
80
|
4 |
|
$respObj = $respObj->{$method}(...$args); |
|
81
|
|
|
} |
|
82
|
4 |
|
respondWith($respObj); |
|
83
|
4 |
|
}; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function respondFrom($method) |
|
87
|
|
|
{ |
|
88
|
|
|
return function () use ($method) { |
|
89
|
1 |
|
respondWith(app()->call(...$method[0])); |
|
|
|
|
|
|
90
|
1 |
|
}; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
public function validatorCallback($rules): \Closure |
|
94
|
|
|
{ |
|
95
|
|
|
$validator = function () use ($rules) { |
|
96
|
2 |
|
if (is_callable($rules)) { |
|
97
|
1 |
|
$rules = $rules(); |
|
98
|
|
|
} |
|
99
|
2 |
|
$validator = \Illuminate\Support\Facades\Validator::make(request()->all(), $rules); |
|
100
|
2 |
|
$validator->validate(); |
|
101
|
5 |
|
}; |
|
102
|
|
|
|
|
103
|
5 |
|
return app(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|