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