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