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 | if ($this->app->runningInConsole()) { |
|
34 | 1 | $this->publishes([ |
|
35 | 1 | __DIR__ . '/../../config/clamav.php' => $this->app->configPath('clamav.php'), |
|
36 | 1 | ], 'config'); |
|
37 | 1 | $this->publishes([ |
|
38 | 1 | __DIR__.'/../lang' => $this->app->resourcePath('lang/vendor/clamav-validator'), |
|
39 | 1 | ], 'lang'); |
|
40 | 1 | $this->app['validator'] |
|
41 | 1 | ->resolver(function ($translator, $data, $rules, $messages, $customAttributes = []) { |
|
42 | 1 | return new ClamavValidator( |
|
43 | 1 | $translator, |
|
44 | 1 | $data, |
|
45 | 1 | $rules, |
|
46 | 1 | $messages, |
|
47 | $customAttributes |
||
48 | 1 | ); |
|
49 | 1 | }); |
|
50 | |||
51 | 1 | $this->addNewRules(); |
|
52 | 1 | } |
|
53 | 1 | } |
|
54 | |||
55 | /** |
||
56 | * Get the list of new rules being added to the validator. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | 1 | public function getRules() |
|
61 | { |
||
62 | 1 | return $this->rules; |
|
63 | } |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Add new rules to the validator. |
||
68 | */ |
||
69 | 1 | protected function addNewRules() |
|
70 | { |
||
71 | 1 | foreach ($this->getRules() as $rule) { |
|
72 | 1 | $this->extendValidator($rule); |
|
73 | 1 | } |
|
74 | 1 | } |
|
75 | |||
76 | |||
77 | /** |
||
78 | * Extend the validator with new rules. |
||
79 | * |
||
80 | * @param string $rule |
||
81 | * @return void |
||
82 | */ |
||
83 | 1 | protected function extendValidator($rule) |
|
84 | { |
||
85 | 1 | $method = studly_case($rule); |
|
86 | 1 | $translation = $this->app['translator']->get('clamav-validator::validation'); |
|
87 | 1 | $this->app['validator']->extend( |
|
88 | 1 | $rule, |
|
89 | 1 | ClamavValidator::class .'@validate' . $method, |
|
90 | 1 | isset($translation[$rule]) ? $translation[$rule] : [] |
|
91 | 1 | ); |
|
92 | 1 | } |
|
93 | |||
94 | |||
95 | /** |
||
96 | * Register the service provider. |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | public function register() |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Get the services provided by the provider. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function provides() |
||
115 | } |
||
116 |