1 | <?php namespace Sunspikes\ClamavValidator; |
||
5 | class ClamavValidatorServiceProvider extends ServiceProvider |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * Indicates if loading of the provider is deferred. |
||
10 | * |
||
11 | * @var bool |
||
12 | */ |
||
13 | protected $defer = false; |
||
14 | |||
15 | /** |
||
16 | * The list of validator rules. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $rules = [ |
||
21 | 'clamav', |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Bootstrap the application events. |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | 1 | public function boot() |
|
30 | { |
||
31 | 1 | $this->loadTranslationsFrom(__DIR__ . '/../lang', 'clamav-validator'); |
|
32 | |||
33 | 1 | $this->publishes([ |
|
34 | 1 | __DIR__ . '/../../config/clamav.php' => $this->app->configPath('clamav.php'), |
|
35 | 1 | ], 'config'); |
|
36 | 1 | $this->publishes([ |
|
37 | 1 | __DIR__.'/../lang' => $this->app->resourcePath('lang/vendor/clamav-validator'), |
|
38 | 1 | ], 'lang'); |
|
39 | 1 | $this->app['validator'] |
|
40 | 1 | ->resolver(function ($translator, $data, $rules, $messages, $customAttributes = []) { |
|
41 | 1 | return new ClamavValidator( |
|
42 | 1 | $translator, |
|
43 | 1 | $data, |
|
44 | 1 | $rules, |
|
45 | 1 | $messages, |
|
46 | $customAttributes |
||
47 | 1 | ); |
|
48 | 1 | }); |
|
49 | |||
50 | 1 | $this->addNewRules(); |
|
51 | 1 | } |
|
52 | |||
53 | /** |
||
54 | * Get the list of new rules being added to the validator. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 1 | public function getRules() |
|
59 | { |
||
60 | 1 | return $this->rules; |
|
61 | } |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Add new rules to the validator. |
||
66 | */ |
||
67 | 1 | protected function addNewRules() |
|
68 | { |
||
69 | 1 | foreach ($this->getRules() as $rule) { |
|
70 | 1 | $this->extendValidator($rule); |
|
71 | 1 | } |
|
72 | 1 | } |
|
73 | |||
74 | |||
75 | /** |
||
76 | * Extend the validator with new rules. |
||
77 | * |
||
78 | * @param string $rule |
||
79 | * @return void |
||
80 | */ |
||
81 | 1 | protected function extendValidator($rule) |
|
82 | { |
||
83 | 1 | $method = studly_case($rule); |
|
84 | 1 | $translation = $this->app['translator']->get('clamav-validator::validation'); |
|
85 | 1 | $this->app['validator']->extend( |
|
86 | 1 | $rule, |
|
87 | 1 | ClamavValidator::class .'@validate' . $method, |
|
88 | 1 | isset($translation[$rule]) ? $translation[$rule] : [] |
|
89 | 1 | ); |
|
90 | 1 | } |
|
91 | |||
92 | |||
93 | /** |
||
94 | * Register the service provider. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | 1 | public function register() |
|
102 | |||
103 | |||
104 | /** |
||
105 | * Get the services provided by the provider. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function provides() |
||
113 | } |
||
114 |