Completed
Push — master ( 6f7fc0...0376f1 )
by Colin
10:19
created

ImageValidatorServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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);
0 ignored issues
show
Documentation introduced by
$messages is of type object<Illuminate\Contra...tion\Translator>|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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