| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Providers;
- use App\Extensions\ValidatorExtension;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Str;
- class AppServiceProvider extends ServiceProvider
- {
- /**
- * @var ValidatorExtension
- */
- protected $validatorExtension = null;
- /**
- * Register any application services.
- *
- * @return void
- */
- public function register()
- {
- if ($this->app->isLocal()) {
- $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
- }
- }
- /**
- * Bootstrap any application services.
- *
- * @return void
- */
- public function boot()
- {
- // 实施验证类扩展
- $this->validatorExtensions();
- }
- /**
- * 实施验证类扩展
- */
- private function validatorExtensions()
- {
- foreach ($this->getValidatorRules() as $method) {
- $method = Str::snake($method);
- Validator::extend($method, function ($arruibute, $value, $paramters, $validator) use ($method) {
- return $this->getValidatorExtension()->{$method}($arruibute, $value, $paramters, $validator);
- });
- }
- }
- /**
- * 获取扩展验证类实例
- * @return ValidatorExtension
- */
- private function getValidatorExtension()
- {
- if (!$this->validatorExtension instanceof ValidatorExtension) {
- $this->validatorExtension = new ValidatorExtension();
- }
- return $this->validatorExtension;
- }
- /**
- * 获取验证实施规则
- * @return array
- */
- private function getValidatorRules()
- {
- return [
- 'mobile',
- ];
- }
- }
|