Blame view

app/Providers/ValidationServiceProvider.php 1.46 KB
b7c7a5f6   Alexey Boroda   first commit
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
  <?php
  
  namespace App\Providers;
  
  use App\Models\Setting\Currency;
  use Illuminate\Support\ServiceProvider;
  use Validator;
  
  class ValidationServiceProvider extends ServiceProvider
  {
      /**
       * Bootstrap any application services.
       *
       * @return void
       */
      public function boot()
      {
          $currency_code = null;
  
          Validator::extend('currency', function ($attribute, $value, $parameters, $validator) use(&$currency_code) {
              $status = false;
              
              if (!is_string($value) || (strlen($value) != 3)) {
                  return $status;
              }
  
              $currencies = Currency::enabled()->pluck('code')->toArray();
  
              if (in_array($value, $currencies)) {
                  $status = true;
              }
  
              $currency_code = $value;
  
              return $status;
          },
              trans('validation.custom.invalid_currency', ['attribute' => $currency_code])
          );
  
          $amount = null;
  
          Validator::extend('amount', function ($attribute, $value, $parameters, $validator) use (&$amount) {
              $status = false;
  
              if ($value > 0) {
                  $status = true;
              }
  
              $amount = $value;
  
              return $status;
          },
              trans('validation.custom.invalid_amount', ['attribute' => $amount])
          );
      }
  
      /**
       * Register any application services.
       *
       * @return void
       */
      public function register()
      {
          //
      }
  }