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

RuleParser::getJsAttributeName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
nc 2
cc 2
eloc 5
nop 1
1
<?php
2
namespace Proengsoft\JsValidation\Javascript;
3
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
    /**
20
     * Token used to secure romte validations
21
     *
22
     * @string|null $remoteToken
23
     */
24
    protected $remoteToken;
25
26
    /**
27
     * Create a new JsValidation instance.
28
     *
29
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
30
     * @param string|null $remoteToken
31
     */
32
    public function __construct(DelegatedValidator $validator, $remoteToken = null)
33
    {
34
        $this->validator = $validator;
35
        $this->remoteToken = $remoteToken;
36
    }
37
38
39
    /**
40
     * Return parsed Javascript Rule.
41
     *
42
     * @param string $attribute
43
     * @param string $rule
44
     * @param $parameters
45
     *
46
     * @return array
47
     */
48
    public function getJsRule($attribute, $rule, $parameters)
49
    {
50
51
        if ($this->isRemoteRule($rule)) {
52
            list($attribute, $parameters) = $this->jsRemoteRule($attribute);
53
            $jsRule = self::REMOTE_RULE;
54
        } else {
55
            list($jsRule, $attribute, $parameters) = $this->jsClientRule($attribute, $rule, $parameters);
56
        }
57
58
        $attribute = $this->getJsAttributeName($attribute);
59
60
        return [$attribute, $jsRule, $parameters];
61
    }
62
63
    /**
64
     * Parses raw rule
65
     *
66
     * @param array|string $rawRule
67
     * @return array
68
     */
69
    public function parseRaw($rawRule) {
70
        return $this->validator->parseRule($rawRule);
71
    }
72
73
    /**
74
     * Gets rules
75
     *
76
     * @return array
77
     */
78
    public function getRules() {
79
        return $this->validator->getRules();
80
    }
81
82
    /**
83
     * Returns Javascript parameters for remote validated rules.
84
     *
85
     * @param string $attribute
86
     * @param $rule
87
     * @param $parameters
88
     *
89
     * @return array
90
     */
91
    protected function jsClientRule($attribute, $rule, $parameters)
92
    {
93
        $jsRule = false;
94
        $method = "jsRule{$rule}";
95
        if (method_exists($this, $method)) {
96
            list($attribute, $parameters) = $this->$method($attribute, $parameters);
97
            $jsRule = 'laravelValidation';
98
        } elseif ($this->jsImplementedRule($rule)) {
99
            $jsRule = 'laravelValidation';
100
        }
101
102
        return [$jsRule, $attribute, $parameters];
103
    }
104
105
    /**
106
     * Returns Javascript parameters for remote validated rules.
107
     *
108
     * @param string $attribute
109
     *
110
     * @return array
111
     */
112
    protected function jsRemoteRule($attribute)
113
    {
114
        $params = [
115
            $attribute,
116
            $this->remoteToken,
117
        ];
118
119
        return [$attribute, $params];
120
    }
121
122
    /**
123
     * Handles multidimensional attribute names.
124
     *
125
     * @param $attribute
126
     *
127
     * @return string
128
     */
129
    protected function getJsAttributeName($attribute)
130
    {
131
        $attributeArray = explode('.', $attribute);
132
        if (count($attributeArray) > 1) {
133
            return $attributeArray[0].'['.implode('][', array_slice($attributeArray, 1)).']';
134
        }
135
136
        return $attribute;
137
    }
138
139
140
    
141
}