Completed
Branch delegated-factory (7f020d)
by Albert
03:05
created

ValidatorHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
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
    /**
22
     * @var RuleParser
23
     */
24
    protected $rules;
25
    /**
26
     * @var MessageParser
27
     */
28
    protected $messages;
29
30
    /**
31
     * Create a new JsValidation instance.
32
     *
33
     * @param RuleParser $rules
34
     * @param MessageParser $messages
35
     */
36
    public function __construct(RuleParser $rules, MessageParser $messages)
37
    {
38
        $this->rules = $rules;
39
        $this->messages = $messages;
40
        $this->validator = $rules->getDelegatedValidator();
41
42
    }
43
44
    /**
45
     * Sets delegated Validator instance.
46
     *
47
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
48
     */
49
    public function setDelegatedValidator(DelegatedValidator $validator) {
50
        $this->validator = $validator;
51
        $this->rules->setDelegatedValidator($validator);
52
        $this->messages->setDelegatedValidator($validator);
53
    }
54
55
56
    protected function generateJavascriptValidations($includeRemote = true)
57
    {
58
        $jsValidations = array();
59
60
        foreach ($this->validator->getRules() as $attribute => $rules) {
61
            if (!$this->jsValidationEnabled($attribute)) continue;
62
63
            $newRules = $this->jsConvertRules($attribute, $rules, $includeRemote);
64
            $jsValidations = array_merge($jsValidations, $newRules);
65
        }
66
67
        return $jsValidations;
68
    }
69
70
    /**
71
     * Make Laravel Validations compatible with JQuery Validation Plugin.
72
     *
73
     * @param $attribute
74
     * @param $rules
75
     * @param $includeRemote
76
     *
77
     * @return array
78
     */
79
    protected function jsConvertRules($attribute, $rules, $includeRemote)
80
    {
81
82
        $jsRules = [];
83
        foreach ($rules as $rawRule) {
84
            list($rule, $parameters) = $this->validator->parseRule($rawRule);
85
            list($jsAttribute, $jsRule, $jsParams) = $this->rules->getRule($attribute, $rule, $parameters);
86
            if ($this->isValidatable($jsRule, $includeRemote)) {
87
                $jsRules[$jsAttribute][$jsRule][] = array($rule, $jsParams,
88
                    $this->messages->getMessage($attribute, $rule, $parameters),
89
                    $this->validator->isImplicit($rule),
90
                );
91
            }
92
        }
93
94
        return $jsRules;
95
    }
96
97
    /**
98
     * Check if rule should be validated with javascript
99
     *
100
     * @param $jsRule
101
     * @param $includeRemote
102
     * @return bool
103
     */
104
    protected function isValidatable($jsRule, $includeRemote) {
105
        return ($jsRule && ($includeRemote || $jsRule != RuleParser::REMOTE_RULE));
106
    }
107
108
109
    /**
110
     * Check if JS Validation is disabled for attribute.
111
     *
112
     * @param $attribute
113
     *
114
     * @return bool
115
     */
116
    public function jsValidationEnabled($attribute)
117
    {
118
        return !$this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE);
119
    }
120
121
    /**
122
     * Returns view data to render javascript.
123
     *
124
     * @param bool $remote
125
     * @return array
126
     */
127
    public function validationData($remote=true)
128
    {
129
        $jsMessages = array();
130
        $jsValidations = $this->generateJavascriptValidations($remote);
131
132
        return [
133
            'rules' => $jsValidations,
134
            'messages' => $jsMessages,
135
        ];
136
    }
137
138
139
140
}
141