33namespace Milwad \LaravelValidate ;
44
55use Illuminate \Support \Facades \File ;
6+ use Illuminate \Support \Facades \Validator ;
67use Illuminate \Support \ServiceProvider ;
8+ use Illuminate \Support \Str ;
79use Milwad \LaravelValidate \Utils \CountryPhoneCallback ;
810
911class LaravelValidateServiceProvider extends ServiceProvider
@@ -30,6 +32,10 @@ protected function publishLangFiles(): void
3032 $ langs = File::directories (__DIR__ .'/lang ' );
3133
3234 foreach ($ langs as $ lang ) {
35+ $ lang = Str::after ($ lang , 'lang ' );
36+ $ lang = Str::replace ('\\' , '' , $ lang );
37+ $ lang = Str::replace ('/ ' , '' , $ lang );
38+
3339 $ this ->publishes ([
3440 __DIR__ ."/lang/ $ lang " => lang_path ($ lang ),
3541 ], "validate-lang- $ lang " );
@@ -58,5 +64,21 @@ public function boot(): void
5864 foreach ($ countries as $ code => $ country ) {
5965 CountryPhoneCallback::addValidator ($ code , $ country );
6066 }
67+
68+ // Register rules in container
69+ if (config ('laravel-validate.using_container ' , false )) {
70+ $ rules = File::files (__DIR__ .'/Rules ' );
71+
72+ foreach ($ rules as $ rule ) {
73+ $ className = 'Milwad \\LaravelValidate \\Rules \\' .$ rule ->getBasename ('.php ' );
74+
75+ Validator::extend (
76+ $ rule ->getFilenameWithoutExtension (),
77+ function ($ attribute , $ value , $ parameters , $ validator ) use ($ className ) {
78+ return (new $ className ($ parameters ))->passes ($ attribute , $ value );
79+ }
80+ );
81+ }
82+ }
6183 }
6284}
0 commit comments