Completed
Push — master ( 328c23...368b19 )
by
unknown
07:01 queued 12s
created

ValidatorHandler::sometimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

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