|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Milwad\LaravelValidate; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
use Illuminate\Support\ServiceProvider; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Milwad\LaravelValidate\Utils\CountryLandlineCallback; |
|
10
|
|
|
use Milwad\LaravelValidate\Utils\CountryPhoneCallback; |
|
11
|
|
|
|
|
12
|
|
|
class LaravelValidateServiceProvider extends ServiceProvider |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Register files. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function register(): void |
|
18
|
|
|
{ |
|
19
|
|
|
if ($this->app->runningInConsole()) { |
|
20
|
|
|
$this->publishLangFiles(); |
|
21
|
|
|
$this->publishConfigFile(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$this->loadTranslationsFrom(__DIR__.'/lang', 'validation'); |
|
25
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/laravel-validate.php', 'laravel-validate'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Publish lang files. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function publishLangFiles(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$langs = File::directories(__DIR__.'/lang'); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($langs as $lang) { |
|
36
|
|
|
$lang = Str::after($lang, 'lang'); |
|
37
|
|
|
$lang = Str::replace('\\', '', $lang); |
|
38
|
|
|
$lang = Str::replace('/', '', $lang); |
|
39
|
|
|
|
|
40
|
|
|
$this->publishes([ |
|
41
|
|
|
__DIR__."/lang/$lang" => lang_path($lang), |
|
42
|
|
|
], "validate-lang-$lang"); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Publish config file. |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function publishConfigFile(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->publishes([ |
|
52
|
|
|
__DIR__.'/../config/laravel-validate.php' => config_path('laravel-validate.php'), |
|
53
|
|
|
], 'laravel-validate-config'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Boot applications. |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Throwable |
|
60
|
|
|
*/ |
|
61
|
|
|
public function boot(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$countries = config('laravel-validate.phone-country', []); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($countries as $code => $country) { |
|
66
|
|
|
CountryPhoneCallback::addValidator($code, $country); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$landlineCountries = config('laravel-validate.landline-country', []); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($landlineCountries as $code => $country) { |
|
72
|
|
|
CountryLandlineCallback::addValidator($code, $country); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Register rules in container |
|
76
|
|
|
if (config('laravel-validate.using_container', false)) { |
|
77
|
|
|
$rules = File::files(__DIR__.'/Rules'); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($rules as $rule) { |
|
80
|
|
|
$className = 'Milwad\\LaravelValidate\\Rules\\'.$rule->getBasename('.php'); |
|
81
|
|
|
|
|
82
|
|
|
Validator::extend( |
|
83
|
|
|
$rule->getFilenameWithoutExtension(), |
|
84
|
|
|
function ($attribute, $value, $parameters, $validator) use ($className) { |
|
|
|
|
|
|
85
|
|
|
return (new $className($parameters))->passes($attribute, $value); |
|
86
|
|
|
} |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.