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

RuleParser::addConditionalRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Proengsoft\JsValidation\Support\DelegatedValidator;
6
use Proengsoft\JsValidation\Support\RuleListTrait;
7
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
8
9
class RuleParser
10
{
11
    use RuleListTrait, JavascriptRulesTrait, UseDelegatedValidatorTrait;
12
13
    /**
14
     * Rule used to validate remote requests.
15
     */
16
    const REMOTE_RULE = 'laravelValidationRemote';
17
18
    /**
19
     * Rule used to validate javascript fields.
20
     */
21
    const JAVASCRIPT_RULE = 'laravelValidation';
22
23
    /**
24
     * Token used to secure romte validations.
25
     *
26
     * @string|null $remoteToken
27
     */
28
    protected $remoteToken;
29
30
    /**
31
     * Conditional Validations.
32
     *
33
     * @var array
34
     */
35
    protected $conditional = [];
36
37
    /**
38
     * Create a new JsValidation instance.
39
     *
40
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
41
     * @param string|null $remoteToken
42
     */
43 14
    public function __construct(DelegatedValidator $validator, $remoteToken = null)
44
    {
45 14
        $this->validator = $validator;
46 14
        $this->remoteToken = $remoteToken;
47 14
    }
48
49
    /**
50
     * Return parsed Javascript Rule.
51
     *
52
     * @param string $attribute
53
     * @param string $rule
54
     * @param $parameters
55
     * @param $rawRule
56
     *
57
     * @return array
58
     */
59 5
    public function getRule($attribute, $rule, $parameters, $rawRule)
60
    {
61 5
        $isConditional = $this->isConditionalRule($attribute, $rawRule);
62 5
        $isRemote = $this->isRemoteRule($rule);
63
64 5
        if ($isConditional || $isRemote) {
65 2
            list($attribute, $parameters) = $this->remoteRule($attribute, $isConditional);
66 2
            $jsRule = self::REMOTE_RULE;
67 2
        } else {
68 3
            list($jsRule, $attribute, $parameters) = $this->clientRule($attribute, $rule, $parameters);
69
        }
70
71 5
        $attribute = $this->getAttributeName($attribute);
72
73 5
        return [$attribute, $jsRule, $parameters];
74
    }
75
76
    /**
77
     * Gets rules from Validator instance.
78
     *
79
     * @return array
80
     */
81 1
    public function getValidatorRules()
82
    {
83 1
        return $this->validator->getRules();
84
    }
85
86
    /**
87
     * Add conditional rules.
88
     *
89
     * @param $attribute
90
     * @param array $rules
91
     */
92 1
    public function addConditionalRules($attribute, $rules = [])
93
    {
94 1
        foreach ((array) $attribute as $key) {
95 1
            $current = isset($this->conditional[$key]) ? $this->conditional[$key] : [];
96 1
            $merge = head($this->validator->explodeRules([$rules]));
97 1
            $this->conditional[$key] = array_merge($current, $merge);
98 1
        }
99 1
    }
100
101
    /**
102
     * Determine if rule is passed with sometimes.
103
     *
104
     * @param $attribute
105
     * @param $rule
106
     * @return bool
107
     */
108 5
    protected function isConditionalRule($attribute, $rule)
109
    {
110 5
        return isset($this->conditional[$attribute]) &&
111 5
        in_array($rule, $this->conditional[$attribute]);
112
    }
113
114
    /**
115
     * Returns Javascript parameters for remote validated rules.
116
     *
117
     * @param string $attribute
118
     * @param $rule
119
     * @param $parameters
120
     *
121
     * @return array
122
     */
123 3
    protected function clientRule($attribute, $rule, $parameters)
124
    {
125 3
        $jsRule = self::JAVASCRIPT_RULE;
126 3
        $method = "rule{$rule}";
127
128 3
        if (method_exists($this, $method)) {
129 1
            list($attribute, $parameters) = $this->$method($attribute, $parameters);
130 1
        }
131
132 3
        return [$jsRule, $attribute, $parameters];
133
    }
134
135
    /**
136
     * Returns Javascript parameters for remote validated rules.
137
     *
138
     * @param string $attribute
139
     * @param bool $forceRemote
140
     *
141
     * @return array
142
     */
143 2
    protected function remoteRule($attribute, $forceRemote)
144
    {
145
        $params = [
146 2
            $attribute,
147 2
            $this->remoteToken,
148 2
            $forceRemote,
149 2
        ];
150
151 2
        return [$attribute, $params];
152
    }
153
154
    /**
155
     * Handles multidimensional attribute names.
156
     *
157
     * @param $attribute
158
     *
159
     * @return string
160
     */
161 5
    protected function getAttributeName($attribute)
162
    {
163 5
        $attributeArray = explode('.', $attribute);
164 5
        if (count($attributeArray) > 1) {
165 1
            return $attributeArray[0].'['.implode('][', array_slice($attributeArray, 1)).']';
166
        }
167
168 4
        return $attribute;
169
    }
170
}
171