Completed
Push — master ( c73a7c...2189c9 )
by Albert
02:08
created

ValidatorHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 5 Features 0
Metric Value
wmc 15
c 10
b 5
f 0
lcom 1
cbo 4
dl 0
loc 160
ccs 47
cts 47
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setDelegatedValidator() 0 6 1
A setRemote() 0 4 1
A generateJavascriptValidations() 0 15 3
A jsConvertRules() 0 16 3
A isValidatable() 0 4 3
A jsValidationEnabled() 0 4 1
A validationData() 0 10 1
A sometimes() 0 6 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Illuminate\Validation\Validator;
6
use Proengsoft\JsValidation\Support\DelegatedValidator;
7
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
8
9
class ValidatorHandler
10
{
11
    use UseDelegatedValidatorTrait;
12
13
    /**
14
     * Rule used to disable validations.
15
     *
16
     * @const string
17
     */
18
    const JSVALIDATION_DISABLE = 'NoJsValidation';
19
20
    /**
21
     * @var RuleParser
22
     */
23
    protected $rules;
24
    /**
25
     * @var MessageParser
26
     */
27
    protected $messages;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $remote = true;
33
34
    /**
35
     * Create a new JsValidation instance.
36
     *
37
     * @param RuleParser $rules
38
     * @param MessageParser $messages
39
     */
40 8
    public function __construct(RuleParser $rules, MessageParser $messages)
41
    {
42 8
        $this->rules = $rules;
43 8
        $this->messages = $messages;
44 8
        $this->validator = $rules->getDelegatedValidator();
45 8
    }
46
47
    /**
48
     * Sets delegated Validator instance.
49
     *
50
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
51
     */
52 4
    public function setDelegatedValidator(DelegatedValidator $validator)
53
    {
54 4
        $this->validator = $validator;
55 4
        $this->rules->setDelegatedValidator($validator);
56 4
        $this->messages->setDelegatedValidator($validator);
57 4
    }
58
59
    /**
60
     *  Enable or disables remote validations.
61
     *
62
     * @param bool $enabled
63
     */
64 1
    public function setRemote($enabled)
65
    {
66 1
        $this->remote = $enabled;
67 1
    }
68
69
    /**
70
     * Generate Javascript Validations.
71
     *
72
     * @return array
73
     */
74 4
    protected function generateJavascriptValidations()
75
    {
76 4
        $jsValidations = array();
77
78 4
        foreach ($this->validator->getRules() as $attribute => $rules) {
79 4
            if (! $this->jsValidationEnabled($attribute)) {
80 1
                continue;
81
            }
82
83 3
            $newRules = $this->jsConvertRules($attribute, $rules, $this->remote);
84 3
            $jsValidations = array_merge($jsValidations, $newRules);
85 4
        }
86
87 4
        return $jsValidations;
88
    }
89
90
    /**
91
     * Make Laravel Validations compatible with JQuery Validation Plugin.
92
     *
93
     * @param $attribute
94
     * @param $rules
95
     * @param bool $includeRemote
96
     *
97
     * @return array
98
     */
99 3
    protected function jsConvertRules($attribute, $rules, $includeRemote)
100
    {
101 3
        $jsRules = [];
102 3
        foreach ($rules as $rawRule) {
103 3
            list($rule, $parameters) = $this->validator->parseRule($rawRule);
104 3
            list($jsAttribute, $jsRule, $jsParams) = $this->rules->getRule($attribute, $rule, $parameters, $rawRule);
105 3
            if ($this->isValidatable($jsRule, $includeRemote)) {
106 2
                $jsRules[$jsAttribute][$jsRule][] = array($rule, $jsParams,
107 2
                    $this->messages->getMessage($attribute, $rule, $parameters),
108 2
                    $this->validator->isImplicit($rule),
109
                );
110 2
            }
111 3
        }
112
113 3
        return $jsRules;
114
    }
115
116
    /**
117
     * Check if rule should be validated with javascript.
118
     *
119
     * @param $jsRule
120
     * @param bool $includeRemote
121
     * @return bool
122
     */
123 3
    protected function isValidatable($jsRule, $includeRemote)
124
    {
125 3
        return $jsRule && ($includeRemote || $jsRule !== RuleParser::REMOTE_RULE);
126
    }
127
128
    /**
129
     * Check if JS Validation is disabled for attribute.
130
     *
131
     * @param $attribute
132
     *
133
     * @return bool
134
     */
135 4
    public function jsValidationEnabled($attribute)
136
    {
137 4
        return ! $this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE);
138
    }
139
140
    /**
141
     * Returns view data to render javascript.
142
     *
143
     * @return array
144
     */
145 4
    public function validationData()
146
    {
147 4
        $jsMessages = array();
148 4
        $jsValidations = $this->generateJavascriptValidations();
149
150
        return [
151 4
            'rules' => $jsValidations,
152 4
            'messages' => $jsMessages,
153 4
        ];
154
    }
155
156
    /**
157
     * Validate Conditional Validations using Ajax in specified fields.
158
     *
159
     * @param  string  $attribute
160
     * @param  string|array  $rules
161
     */
162
    public function sometimes($attribute, $rules = [])
163
    {
164
        $callback = function () {return true; };
165 1
        $this->validator->sometimes($attribute, $rules, $callback);
166 1
        $this->rules->addConditionalRules($attribute, $rules);
0 ignored issues
show
Bug introduced by
It seems like $rules defined by parameter $rules on line 162 can also be of type string; however, Proengsoft\JsValidation\...::addConditionalRules() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
167 1
    }
168
}
169