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