AppServiceProvider.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Providers;
  3. use App\Extensions\ValidatorExtension;
  4. use Illuminate\Support\ServiceProvider;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Str;
  7. class AppServiceProvider extends ServiceProvider
  8. {
  9. /**
  10. * @var ValidatorExtension
  11. */
  12. protected $validatorExtension = null;
  13. /**
  14. * Register any application services.
  15. *
  16. * @return void
  17. */
  18. public function register()
  19. {
  20. if ($this->app->isLocal()) {
  21. $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
  22. }
  23. }
  24. /**
  25. * Bootstrap any application services.
  26. *
  27. * @return void
  28. */
  29. public function boot()
  30. {
  31. // 实施验证类扩展
  32. $this->validatorExtensions();
  33. }
  34. /**
  35. * 实施验证类扩展
  36. */
  37. private function validatorExtensions()
  38. {
  39. foreach ($this->getValidatorRules() as $method) {
  40. $method = Str::snake($method);
  41. Validator::extend($method, function ($arruibute, $value, $paramters, $validator) use ($method) {
  42. return $this->getValidatorExtension()->{$method}($arruibute, $value, $paramters, $validator);
  43. });
  44. }
  45. }
  46. /**
  47. * 获取扩展验证类实例
  48. * @return ValidatorExtension
  49. */
  50. private function getValidatorExtension()
  51. {
  52. if (!$this->validatorExtension instanceof ValidatorExtension) {
  53. $this->validatorExtension = new ValidatorExtension();
  54. }
  55. return $this->validatorExtension;
  56. }
  57. /**
  58. * 获取验证实施规则
  59. * @return array
  60. */
  61. private function getValidatorRules()
  62. {
  63. return [
  64. 'mobile',
  65. ];
  66. }
  67. }