1 | <?php |
||
16 | class UnknownValidationSolutionProvider implements HasSolutionsForThrowable |
||
17 | { |
||
18 | protected const REGEX = '/Illuminate\\\\Validation\\\\Validator::(?P<method>validate(?!(Attribute|UsingCustomRule))[A-Z][a-zA-Z]+)/m'; |
||
19 | |||
20 | public function canSolve(Throwable $throwable): bool |
||
21 | { |
||
22 | if (! $throwable instanceof BadMethodCallException) { |
||
23 | return false; |
||
24 | } |
||
25 | |||
26 | return ! is_null($this->getMethodFromExceptionMessage($throwable->getMessage())); |
||
27 | } |
||
28 | |||
29 | public function getSolutions(Throwable $throwable): array |
||
30 | { |
||
31 | return [ |
||
32 | BaseSolution::create('Unknown Validation Rule') |
||
33 | ->setSolutionDescription($this->getSolutionDescription($throwable)), |
||
34 | ]; |
||
35 | } |
||
36 | |||
37 | protected function getSolutionDescription(Throwable $throwable): string |
||
38 | { |
||
39 | $method = $this->getMethodFromExceptionMessage($throwable->getMessage()); |
||
40 | |||
41 | $possibleMethod = StringComparator::findSimilarText( |
||
42 | $this->getAvailableMethods()->toArray(), |
||
43 | $method |
||
44 | ); |
||
45 | |||
46 | if (empty($possibleMethod)) { |
||
47 | return ''; |
||
48 | } |
||
49 | |||
50 | $rule = Str::snake(str_replace('validate', '', $possibleMethod)); |
||
51 | |||
52 | return "Did you mean `{$rule}` ?"; |
||
53 | } |
||
54 | |||
55 | protected function getMethodFromExceptionMessage(string $message): ?string |
||
56 | { |
||
57 | if (! preg_match(self::REGEX, $message, $matches)) { |
||
58 | return null; |
||
59 | } |
||
60 | |||
61 | return $matches['method']; |
||
62 | } |
||
63 | |||
64 | protected function getAvailableMethods(): Collection |
||
83 | } |
||
84 |