|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Pixelpeter\IsoCodesValidation;
|
4 | 6 |
|
5 | 7 | use Illuminate\Support\ServiceProvider;
|
6 |
| -use Illuminate\Support\Str; |
| 8 | +use Pixelpeter\IsoCodesValidation\Support\ReferenceResolver; |
| 9 | +use Pixelpeter\IsoCodesValidation\Support\Registry; |
| 10 | +use Pixelpeter\IsoCodesValidation\Support\Replacer; |
7 | 11 |
|
8 |
| -class IsoCodesValidationServiceProvider extends ServiceProvider |
| 12 | +final class IsoCodesValidationServiceProvider extends ServiceProvider |
9 | 13 | {
|
10 |
| - /** |
11 |
| - * Bootstrap the application services. |
12 |
| - * |
13 |
| - * @return void |
14 |
| - */ |
15 |
| - public function boot() |
| 14 | + public function boot(): void |
16 | 15 | {
|
17 |
| - // load translation files |
18 | 16 | $this->loadTranslationsFrom(
|
19 | 17 | __DIR__.'/../lang',
|
20 | 18 | 'validation'
|
21 | 19 | );
|
22 | 20 |
|
23 |
| - // registering intervention validator extension |
24 |
| - $this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) { |
25 |
| - // set the validation error messages |
26 |
| - foreach (get_class_methods('Pixelpeter\IsoCodesValidation\IsoCodesValidator') as $method) { |
27 |
| - $key = $this->getTranslationKeyFromMethodName($method); |
| 21 | + $validatorService = $this->app->make('validator'); |
28 | 22 |
|
29 |
| - $messages[$key] = $this->getErrorMessage($translator, $messages, $key); |
30 |
| - } |
| 23 | + foreach (Registry::ruleNames() as $rule) { |
| 24 | + $validatorService->extend( |
| 25 | + $rule, |
| 26 | + function ($attribute, $value, $parameters, $validator) use ($rule): bool { |
| 27 | + $validatorClass = Registry::validatorClassForRule($rule); |
| 28 | + $referenceResolver = new ReferenceResolver; |
| 29 | + $isoCodesValdator = new IsoCodesValidator($attribute, $value, $parameters, $validator, $rule, $validatorClass, $referenceResolver); |
31 | 30 |
|
32 |
| - return new IsoCodesValidator($translator, $data, $rules, $messages, $customAttributes); |
33 |
| - }); |
34 |
| - } |
| 31 | + return $isoCodesValdator->validate(); |
| 32 | + }, |
| 33 | + $this->errorMessage($rule) |
| 34 | + ); |
35 | 35 |
|
36 |
| - /** |
37 |
| - * Return translation key for correspondent method name |
38 |
| - * |
39 |
| - * @param string $name |
40 |
| - * @return string |
41 |
| - */ |
42 |
| - private function getTranslationKeyFromMethodName($name) |
43 |
| - { |
44 |
| - if (stripos($name, 'validate') !== false) { |
45 |
| - return Str::snake(substr($name, 8)); |
46 |
| - } |
| 36 | + $validatorService->replacer($rule, function ($message, $attribute, $_, $parameters, $validator) { |
| 37 | + $referenceResolver = new ReferenceResolver; |
| 38 | + $replacer = new Replacer($attribute, $parameters, $validator, $referenceResolver); |
47 | 39 |
|
48 |
| - return ''; |
49 |
| - } |
| 40 | + return $replacer->replace($message); |
| 41 | + }); |
50 | 42 |
|
51 |
| - /** |
52 |
| - * Return the matching error message for the key |
53 |
| - * |
54 |
| - * @param string $key |
55 |
| - * @return string |
56 |
| - */ |
57 |
| - private function getErrorMessage($translator, $messages, $key) |
58 |
| - { |
59 |
| - // return error messages passed directly to the validator |
60 |
| - if (isset($messages[$key])) { |
61 |
| - return $messages[$key]; |
62 |
| - } |
63 |
| - |
64 |
| - // return error message from validation translation file |
65 |
| - if ($translator->has("validation.{$key}")) { |
66 |
| - return $translator->get("validation.{$key}"); |
67 | 43 | }
|
68 |
| - |
69 |
| - // return packages default message |
70 |
| - return $translator->get("validation::validation.{$key}"); |
71 | 44 | }
|
72 | 45 |
|
73 |
| - /** |
74 |
| - * Register the application services. |
75 |
| - * |
76 |
| - * @return void |
77 |
| - */ |
78 |
| - public function register() |
| 46 | + protected function errorMessage(string $rulename): string |
79 | 47 | {
|
80 |
| - // |
| 48 | + return $this->app->make('translator')->get('validation::validation.'.$rulename); |
81 | 49 | }
|
82 | 50 | }
|
0 commit comments