ImageValidatorServiceProvider::addNewRules()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php namespace Cviebrock\ImageValidator;
2
3
use Illuminate\Support\ServiceProvider;
4
5
6
class ImageValidatorServiceProvider extends ServiceProvider
7
{
8
9
    /**
10
     * Indicates if loading of the provider is deferred.
11
     *
12
     * @var bool
13
     */
14
    protected $defer = false;
15
16
    /**
17
     * @var array
18
     */
19
    protected $rules = [
20
        'image_size',
21
        'image_aspect',
22
    ];
23
24
    /**
25
     * Bootstrap the application events.
26
     *
27
     * @return void
28
     */
29
    public function boot()
30
    {
31
        $this->loadTranslationsFrom(__DIR__ . '/../lang', 'image-validator');
32
33
        $messages = trans('image-validator::validation');
34
35
        $this->app->bind(ImageValidator::class, function($app) use ($messages) {
36
            $validator = new ImageValidator($app['translator'], [], [], $messages);
37
38
            if (isset($app['validation.presence'])) {
39
                $validator->setPresenceVerifier($app['validation.presence']);
40
            }
41
42
            return $validator;
43
        });
44
45
        $this->addNewRules();
46
    }
47
48
    /**
49
     * Get the list of new rules being added to the validator.
50
     *
51
     * @return array
52
     */
53
    public function getRules()
54
    {
55
        return $this->rules;
56
    }
57
58
    /**
59
     * Add new rules to the validator.
60
     */
61
    protected function addNewRules()
62
    {
63
        foreach ($this->getRules() as $rule) {
64
            $this->extendValidator($rule);
65
        }
66
    }
67
68
    /**
69
     * Extend the validator with new rules.
70
     *
71
     * @param  string $rule
72
     * @return void
73
     */
74
    protected function extendValidator($rule)
75
    {
76
        $method = studly_case($rule);
77
        $translation = $this->app['translator']->trans('image-validator::validation.' . $rule);
78
        $this->app['validator']->extend($rule, 'Cviebrock\ImageValidator\ImageValidator@validate' . $method,
79
            $translation);
80
        $this->app['validator']->replacer($rule, 'Cviebrock\ImageValidator\ImageValidator@replace' . $method);
81
    }
82
}
83