Completed
Pull Request — master (#348)
by
unknown
13:10
created

RuleParser::getRule()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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