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