|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\SolutionProviders; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use Facade\IgnitionContracts\BaseSolution; |
|
7
|
|
|
use Facade\IgnitionContracts\HasSolutionsForThrowable; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use Illuminate\Validation\Validator; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
|
|
15
|
|
|
class UnknownValidationSolutionProvider implements HasSolutionsForThrowable |
|
16
|
|
|
{ |
|
17
|
|
|
protected const REGEX = '/([a-zA-Z\\\\]+)::([a-zA-Z]+)/m'; |
|
18
|
|
|
|
|
19
|
|
|
public function canSolve(Throwable $throwable): bool |
|
20
|
|
|
{ |
|
21
|
|
|
if (! $throwable instanceof BadMethodCallException) { |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if (is_null($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()))) { |
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return true; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getSolutions(Throwable $throwable): array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
BaseSolution::create('Unknown Validation Rule') |
|
36
|
|
|
->setSolutionDescription($this->getSolutionDescription($throwable)), |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getSolutionDescription(Throwable $throwable): string |
|
41
|
|
|
{ |
|
42
|
|
|
if (! $this->canSolve($throwable)) { |
|
43
|
|
|
return ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
extract($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()), EXTR_OVERWRITE); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$possibleMethod = $this->findPossibleMethod($class, $method); |
|
49
|
|
|
$rule = strtolower(str_replace('validate', '', $possibleMethod)); |
|
50
|
|
|
|
|
51
|
|
|
return "Did you mean `{$rule}` ?"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getClassAndMethodFromExceptionMessage(string $message): ?array |
|
55
|
|
|
{ |
|
56
|
|
|
if (! preg_match(self::REGEX, $message, $matches)) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($matches[1] !== Validator::class) { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! Str::startsWith($matches[2], 'validate')) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return [ |
|
69
|
|
|
'class' => $matches[1], |
|
70
|
|
|
'method' => $matches[2], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function findPossibleMethod(string $class, string $invalidMethodName) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getAvailableMethods($class) |
|
77
|
|
|
->sortByDesc(function (string $method) use ($invalidMethodName) { |
|
78
|
|
|
similar_text($invalidMethodName, $method, $percentage); |
|
79
|
|
|
|
|
80
|
|
|
return $percentage; |
|
81
|
|
|
})->first(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function getAvailableMethods($class): Collection |
|
85
|
|
|
{ |
|
86
|
|
|
$class = new ReflectionClass($class); |
|
87
|
|
|
|
|
88
|
|
|
$extensions = Collection::make((\Illuminate\Support\Facades\Validator::make([], []))->extensions) |
|
|
|
|
|
|
89
|
|
|
->keys() |
|
90
|
|
|
->map(function (string $extension) { |
|
91
|
|
|
return 'validate' . ucfirst($extension); |
|
92
|
|
|
}); |
|
93
|
|
|
|
|
94
|
|
|
return Collection::make($class->getMethods()) |
|
95
|
|
|
->filter(function (ReflectionMethod $method) { |
|
96
|
|
|
return Str::startsWith($method->name, 'validate') && ! in_array($method->name, [ |
|
97
|
|
|
'validate', |
|
98
|
|
|
'validated', |
|
99
|
|
|
'validateAttribute', |
|
100
|
|
|
'validateUsingCustomRule' |
|
101
|
|
|
]); |
|
102
|
|
|
}) |
|
103
|
|
|
->map(function (ReflectionMethod $method) { |
|
104
|
|
|
return $method->name; |
|
105
|
|
|
}) |
|
106
|
|
|
->merge($extensions); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|