Completed
Branch delegated-factory (7f020d)
by Albert
03:05
created

RuleParser::getJsRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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