Completed
Branch delegated-factory (41b3c7)
by Albert
02:04
created

ValidatorParser::generateJavascriptValidations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
nc 3
cc 3
eloc 7
nop 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 ValidatorParser
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->getJsRule($attribute, $rule, $parameters);
86
            if ($jsRule && ($includeRemote || $jsRule != RuleParser::REMOTE_RULE)) {
87
                $jsRules[$jsAttribute][$jsRule][] = array($rule, $jsParams,
88
                    $this->messages->getJsMessage($attribute, $rule, $parameters),
89
                    $this->validator->isImplicit($rule),
90
                );
91
            }
92
        }
93
94
        return $jsRules;
95
    }
96
97
98
    /**
99
     * Check if JS Validation is disabled for attribute.
100
     *
101
     * @param $attribute
102
     *
103
     * @return bool
104
     */
105
    public function jsValidationEnabled($attribute)
106
    {
107
        return !$this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE);
108
    }
109
110
    /**
111
     * Returns view data to render javascript.
112
     *
113
     * @param bool $remote
114
     * @return array
115
     */
116
    public function validationData($remote=true)
117
    {
118
        $jsMessages = array();
119
        $jsValidations = $this->generateJavascriptValidations($remote);
120
121
        return [
122
            'rules' => $jsValidations,
123
            'messages' => $jsMessages,
124
        ];
125
    }
126
127
128
129
}
130