1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan\Reactions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Factory; |
6
|
|
|
use Imanghafoori\HeyMan\Core\Reaction; |
7
|
|
|
use Imanghafoori\HeyMan\Switching\HeyManSwitcher; |
8
|
|
|
|
9
|
|
|
final class Validator |
10
|
|
|
{ |
11
|
|
|
private $validationData; |
12
|
|
|
|
13
|
|
|
private $modifier; |
14
|
|
|
|
15
|
12 |
|
public function __construct(array $validationData) |
16
|
|
|
{ |
17
|
12 |
|
$this->validationData = $validationData; |
18
|
12 |
|
} |
19
|
|
|
|
20
|
3 |
|
public function otherwise() |
21
|
|
|
{ |
22
|
3 |
|
$rules = $this->validationData; |
23
|
3 |
|
$result = $this->validationPassesCallback($this->getModifier(), $rules); |
24
|
|
|
|
25
|
3 |
|
resolve('heyman.chain')->set('condition', $result); |
|
|
|
|
26
|
|
|
|
27
|
3 |
|
return resolve(Reaction::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function beforeValidationModifyData($callable) |
31
|
|
|
{ |
32
|
1 |
|
$this->modifier = $callable; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
public function validatorCallback($modifier, $rules) |
36
|
|
|
{ |
37
|
9 |
|
$validator = function () use ($modifier, $rules) { |
38
|
5 |
|
$this->makeValidator($modifier, $rules)->validate(); |
39
|
9 |
|
}; |
40
|
|
|
|
41
|
9 |
|
return $this->wrapForIgnore($validator); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function validationPassesCallback($modifier, $rules) |
45
|
|
|
{ |
46
|
3 |
|
$validator = function () use ($modifier, $rules) { |
47
|
1 |
|
$v = $this->makeValidator($modifier, $rules); |
48
|
|
|
|
49
|
1 |
|
resolve('heyman.chain')->set('condition_meta', $v); |
50
|
|
|
|
51
|
1 |
|
return ! $v->fails(); |
52
|
3 |
|
}; |
53
|
|
|
|
54
|
3 |
|
return $this->wrapForIgnore($validator); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
public function makeValidator($modifier, $rules) |
58
|
|
|
{ |
59
|
6 |
|
if (is_callable($rules[0])) { |
60
|
4 |
|
$rules[0] = call_user_func($rules[0]); |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
$newData = app()->call($modifier, [request()->all()]); |
64
|
|
|
|
65
|
6 |
|
return resolve(Factory::class)->make($newData, ...$rules); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
public function __destruct() |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
10 |
|
$chain = app('heyman.chain'); |
72
|
10 |
|
$condition = $chain->get('condition'); |
73
|
|
|
|
74
|
10 |
|
if ($condition) { |
75
|
2 |
|
return resolve('heyman.chains')->commitChain(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
9 |
|
$data = $this->validationData; |
79
|
9 |
|
$condition = $this->validatorCallback($this->getModifier(), $data); |
80
|
9 |
|
$chain->set('condition', $condition); |
81
|
|
|
|
82
|
9 |
|
resolve('heyman.chains')->commitChain(); |
83
|
|
|
} catch (\Throwable $throwable) { |
84
|
|
|
// |
85
|
|
|
} |
86
|
9 |
|
} |
87
|
|
|
|
88
|
12 |
|
private function wrapForIgnore($validator) |
89
|
|
|
{ |
90
|
12 |
|
return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getModifier() |
94
|
|
|
{ |
95
|
12 |
|
return $this->modifier ?: function () { |
96
|
5 |
|
return request()->all(); |
97
|
12 |
|
}; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.