Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

RuleParser::remoteRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6667
cc 1
eloc 5
nc 1
nop 1
crap 1
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
     * Create a new JsValidation instance.
32
     *
33
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
34
     * @param string|null $remoteToken
35
     */
36 11
    public function __construct(DelegatedValidator $validator, $remoteToken = null)
37
    {
38 11
        $this->validator = $validator;
39 11
        $this->remoteToken = $remoteToken;
40 11
    }
41
42
    /**
43
     * Return parsed Javascript Rule.
44
     *
45
     * @param string $attribute
46
     * @param string $rule
47
     * @param $parameters
48
     *
49
     * @return array
50
     */
51 4
    public function getRule($attribute, $rule, $parameters)
52
    {
53 4
        if ($this->isRemoteRule($rule)) {
54 1
            list($attribute, $parameters) = $this->remoteRule($attribute);
55 1
            $jsRule = self::REMOTE_RULE;
56 1
        } else {
57 3
            list($jsRule, $attribute, $parameters) = $this->clientRule($attribute, $rule, $parameters);
58
        }
59
60 4
        $attribute = $this->getAttributeName($attribute);
61
62 4
        return [$attribute, $jsRule, $parameters];
63
    }
64
65
    /**
66
     * Gets rules.
67
     *
68
     * @return array
69
     */
70 1
    public function getRules()
71
    {
72 1
        return $this->validator->getRules();
73
    }
74
75
    /**
76
     * Returns Javascript parameters for remote validated rules.
77
     *
78
     * @param string $attribute
79
     * @param $rule
80
     * @param $parameters
81
     *
82
     * @return array
83
     */
84 3
    protected function clientRule($attribute, $rule, $parameters)
85
    {
86 3
        $jsRule = self::JAVASCRIPT_RULE;
87 3
        $method = "rule{$rule}";
88
89 3
        if (method_exists($this, $method)) {
90 1
            list($attribute, $parameters) = $this->$method($attribute, $parameters);
91 1
        }
92
93 3
        return [$jsRule, $attribute, $parameters];
94
    }
95
96
    /**
97
     * Returns Javascript parameters for remote validated rules.
98
     *
99
     * @param string $attribute
100
     *
101
     * @return array
102
     */
103 1
    protected function remoteRule($attribute)
104
    {
105
        $params = [
106 1
            $attribute,
107 1
            $this->remoteToken,
108 1
        ];
109
110 1
        return [$attribute, $params];
111
    }
112
113
    /**
114
     * Handles multidimensional attribute names.
115
     *
116
     * @param $attribute
117
     *
118
     * @return string
119
     */
120 4
    protected function getAttributeName($attribute)
121
    {
122 4
        $attributeArray = explode('.', $attribute);
123 4
        if (count($attributeArray) > 1) {
124 1
            return $attributeArray[0].'['.implode('][', array_slice($attributeArray, 1)).']';
125
        }
126
127 3
        return $attribute;
128
    }
129
}
130