|
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
|
9 |
|
public function __construct(array $validationData) |
|
16
|
|
|
{ |
|
17
|
9 |
|
$this->validationData = $validationData; |
|
18
|
9 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function otherwise() |
|
21
|
|
|
{ |
|
22
|
1 |
|
$rules = $this->validationData; |
|
23
|
1 |
|
$modifier = $this->modifier ?: function ($args) { |
|
24
|
1 |
|
return $args; |
|
25
|
1 |
|
}; |
|
26
|
|
|
|
|
27
|
1 |
|
$result = $this->validationPassesCallback($modifier, $rules); |
|
28
|
|
|
|
|
29
|
1 |
|
resolve('heyman.chain')->set('condition', $result); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
1 |
|
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
|
8 |
|
$validator = function () use ($modifier, $rules) { |
|
42
|
5 |
|
$this->makeValidator($modifier, $rules)->validate(); |
|
43
|
8 |
|
}; |
|
44
|
|
|
|
|
45
|
8 |
|
return $this->wrapForIgnore($validator); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function validationPassesCallback($modifier, $rules) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$validator = function () use ($modifier, $rules) { |
|
51
|
1 |
|
return ! $this->makeValidator($modifier, $rules)->fails(); |
|
52
|
1 |
|
}; |
|
53
|
|
|
|
|
54
|
1 |
|
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
|
8 |
|
public function __destruct() |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
8 |
|
$chain = app('heyman.chain'); |
|
72
|
8 |
|
$condition = $chain->get('condition'); |
|
73
|
|
|
|
|
74
|
8 |
|
if (! $condition) { |
|
75
|
8 |
|
$data = $this->validationData; |
|
76
|
8 |
|
$modifier = $this->modifier ?: function ($args) { |
|
77
|
4 |
|
return $args; |
|
78
|
8 |
|
}; |
|
79
|
|
|
|
|
80
|
8 |
|
$condition = $this->validatorCallback($modifier, $data); |
|
81
|
8 |
|
$chain->set('condition', $condition); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
8 |
|
resolve('heyman.chains')->commitChain(); |
|
|
|
|
|
|
85
|
|
|
} catch (\Throwable $throwable) { |
|
86
|
|
|
// |
|
87
|
|
|
} |
|
88
|
8 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param \Closure $validator |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
9 |
|
private function wrapForIgnore(\Closure $validator) |
|
96
|
|
|
{ |
|
97
|
9 |
|
return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation'); |
|
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.