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