1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan\Reactions; |
4
|
|
|
|
5
|
|
|
use Imanghafoori\HeyMan\Core\Reaction; |
6
|
|
|
use Illuminate\Contracts\Validation\Factory; |
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 |
|
$modifier = $this->modifier ?: function ($args) { |
24
|
1 |
|
return $args; |
25
|
3 |
|
}; |
26
|
|
|
|
27
|
3 |
|
$result = $this->validationPassesCallback($modifier, $rules); |
28
|
|
|
|
29
|
3 |
|
resolve('heyman.chain')->set('condition', $result); |
|
|
|
|
30
|
|
|
|
31
|
3 |
|
return resolve(Reaction::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function beforeValidationModifyData($callable) |
35
|
|
|
{ |
36
|
1 |
|
$this->modifier = $callable; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
public function validatorCallback($modifier, $rules) |
40
|
|
|
{ |
41
|
9 |
|
$validator = function () use ($modifier, $rules) { |
42
|
5 |
|
$this->makeValidator($modifier, $rules)->validate(); |
43
|
9 |
|
}; |
44
|
|
|
|
45
|
9 |
|
return $this->wrapForIgnore($validator); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function validationPassesCallback($modifier, $rules) |
49
|
|
|
{ |
50
|
3 |
|
$validator = function () use ($modifier, $rules) { |
51
|
1 |
|
$v = $this->makeValidator($modifier, $rules); |
52
|
|
|
|
53
|
1 |
|
resolve('heyman.chain')->set('condition_meta', $v); |
54
|
|
|
|
55
|
1 |
|
return ! $v->fails(); |
56
|
3 |
|
}; |
57
|
|
|
|
58
|
3 |
|
return $this->wrapForIgnore($validator); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
public function makeValidator($modifier, $rules) |
62
|
|
|
{ |
63
|
6 |
|
if (is_callable($rules[0])) { |
64
|
4 |
|
$rules[0] = call_user_func($rules[0]); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$newData = app()->call($modifier, [request()->all()]); |
68
|
|
|
|
69
|
6 |
|
return resolve(Factory::class)->make($newData, ...$rules); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
public function __destruct() |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
10 |
|
$chain = app('heyman.chain'); |
76
|
10 |
|
$condition = $chain->get('condition'); |
77
|
|
|
|
78
|
10 |
|
if ($condition) { |
79
|
2 |
|
return resolve('heyman.chains')->commitChain(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
$data = $this->validationData; |
83
|
9 |
|
$modifier = $this->modifier ?: function ($args) { |
84
|
4 |
|
return $args; |
85
|
9 |
|
}; |
86
|
|
|
|
87
|
9 |
|
$condition = $this->validatorCallback($modifier, $data); |
88
|
9 |
|
$chain->set('condition', $condition); |
89
|
|
|
|
90
|
9 |
|
resolve('heyman.chains')->commitChain(); |
91
|
|
|
} catch (\Throwable $throwable) { |
92
|
|
|
// |
93
|
|
|
} |
94
|
9 |
|
} |
95
|
|
|
|
96
|
12 |
|
private function wrapForIgnore($validator) |
97
|
|
|
{ |
98
|
12 |
|
return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.