-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIsoCodesValidationServiceProvider.php
80 lines (69 loc) · 2.13 KB
/
IsoCodesValidationServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Pixelpeter\IsoCodesValidation;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class IsoCodesValidationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// load translation files
$this->loadTranslationsFrom(
__DIR__ . '/../lang',
'validation'
);
// registering intervention validator extension
$this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
// set the validation error messages
foreach (get_class_methods('Pixelpeter\IsoCodesValidation\IsoCodesValidator') as $method) {
$key = $this->getTranslationKeyFromMethodName($method);
$messages[$key] = $this->getErrorMessage($translator, $messages, $key);
}
return new IsoCodesValidator($translator, $data, $rules, $messages, $customAttributes);
});
}
/**
* Return translation key for correspondent method name
*
* @param string $name
* @return string
*/
private function getTranslationKeyFromMethodName($name)
{
if (Str::contains($name, 'validate')) {
return Str::snake(Str::after($name, 'validate'));
}
}
/**
* Return the matching error message for the key
*
* @param string $key
* @return string
*/
private function getErrorMessage($translator, $messages, $key)
{
// return error messages passed directly to the validator
if (isset($messages[$key])) {
return $messages[$key];
}
// return error message from validation translation file
if ($translator->has("validation.{$key}")) {
return $translator->get("validation.{$key}");
}
// return packages default message
return $translator->get("validation::validation.{$key}");
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}