BankServiceProvider::loadedValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8666
1
<?php
2
3
namespace SimpleCMS\Bank;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Facades\Validator;
8
use SimpleCMS\Bank\Validation\Rule\BankRule;
9
use SimpleCMS\Bank\Validation\Rule\BankBinRule;
10
use SimpleCMS\Bank\Validation\Rule\BankCardRule;
11
use SimpleCMS\Bank\Validation\Rule\BankCodeRule;
12
13
class BankServiceProvider extends ServiceProvider
14
{
15
16
    /**
17
     * Bootstrap services.
18
     */
19
    public function boot(): void
20
    {
21
        $this->loadedHelpers();
22
        $this->loadFacades();
23
        $this->loadedValidator();
24
    }
25
26
    /**
27
     * 加载验证
28
     *
29
     * @author Dennis Lui <[email protected]>
30
     * @return void
31
     */
32
    protected function loadedValidator(): void
33
    {
34
        Validator::extend(
35
            'bank',
36
            BankRule::class
37
        );
38
        Validator::extend(
39
            'bank_card',
40
            BankCardRule::class
41
        );
42
        Validator::extend(
43
            'bank_code',
44
            BankCodeRule::class
45
        );
46
        Validator::extend(
47
            'bank_bin',
48
            BankBinRule::class
49
        );
50
    }
51
52
53
    /**
54
     * 绑定Facades
55
     *
56
     * @author Dennis Lui <[email protected]>
57
     * @return void
58
     */
59
    protected function loadFacades(): void
60
    {
61
        $this->app->bind('bank', fn() => new \SimpleCMS\Bank\Packages\Bank(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '/data/banks.json'));
62
    }
63
    
64
    /**
65
     * 加载辅助函数
66
     *
67
     * @author Dennis Lui <[email protected]>
68
     * @return void
69
     */
70
    protected function loadedHelpers(): void
71
    {
72
73
        foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'helpers') as $helperFile) {
74
            $path = sprintf(
75
                '%s%s%s%s%s',
76
                __DIR__,
77
                DIRECTORY_SEPARATOR,
78
                'helpers',
79
                DIRECTORY_SEPARATOR,
80
                $helperFile
81
            );
82
83
            if (!is_file($path)) {
84
                continue;
85
            }
86
87
            $function = Str::before($helperFile, '.php');
88
89
            if (function_exists($function)) {
90
                continue;
91
            }
92
93
            require_once $path;
94
        }
95
    }
96
}
97