1 | <?php |
||
2 | |||
3 | namespace Pixelpeter\IsoCodesValidation; |
||
4 | |||
5 | use Illuminate\Support\ServiceProvider; |
||
6 | |||
7 | class IsoCodesValidationServiceProvider extends ServiceProvider |
||
8 | { |
||
9 | /** |
||
10 | * Bootstrap the application services. |
||
11 | * |
||
12 | * @return void |
||
13 | */ |
||
14 | public function boot() |
||
15 | { |
||
16 | // load translation files |
||
17 | $this->loadTranslationsFrom( |
||
18 | __DIR__ . '/../lang', |
||
19 | 'validation' |
||
20 | ); |
||
21 | |||
22 | // registering intervention validator extension |
||
23 | $this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) { |
||
24 | // set the validation error messages |
||
25 | foreach (get_class_methods('Pixelpeter\IsoCodesValidation\IsoCodesValidator') as $method) { |
||
26 | $key = $this->getTranslationKeyFromMethodName($method); |
||
27 | |||
28 | $messages[$key] = $this->getErrorMessage($translator, $messages, $key); |
||
29 | } |
||
30 | |||
31 | return new IsoCodesValidator($translator, $data, $rules, $messages, $customAttributes); |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Return translation key for correspondent method name |
||
37 | * |
||
38 | * @param string $name |
||
39 | * @return string |
||
40 | */ |
||
41 | private function getTranslationKeyFromMethodName($name) |
||
42 | { |
||
43 | if (stripos($name, 'validate') !== false) { |
||
44 | return snake_case(substr($name, 8)); |
||
0 ignored issues
–
show
|
|||
45 | } |
||
46 | |||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return the matching error message for the key |
||
51 | * |
||
52 | * @param string $key |
||
53 | * @return string |
||
54 | */ |
||
55 | private function getErrorMessage($translator, $messages, $key) |
||
56 | { |
||
57 | // return error messages passed directly to the validator |
||
58 | if (isset($messages[$key])) { |
||
59 | return $messages[$key]; |
||
60 | } |
||
61 | |||
62 | // return error message from validation translation file |
||
63 | if ($translator->has("validation.{$key}")) { |
||
64 | return $translator->get("validation.{$key}"); |
||
65 | } |
||
66 | |||
67 | // return packages default message |
||
68 | return $translator->get("validation::validation.{$key}"); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Register the application services. |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | public function register() |
||
77 | { |
||
78 | // |
||
79 | } |
||
80 | } |
||
81 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.