Completed
Branch feature/conditional-validation... (02769e)
by Albert
34:59 queued 25:31
created

RuleParser::getValidatorRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 12
    public function __construct(DelegatedValidator $validator, $remoteToken = null)
37
    {
38 12
        $this->validator = $validator;
39 12
        $this->remoteToken = $remoteToken;
40 12
    }
41
42
    /**
43
     * Return parsed Javascript Rule.
44
     *
45
     * @param string $attribute
46
     * @param string $rule
47
     * @param $parameters
48
     * @param $forceRemote
49
     *
50
     * @return array
51
     */
52 4
    public function getRule($attribute, $rule, $parameters, $forceRemote = false)
53
    {
54 4
        $remote = $forceRemote || $this->isRemoteRule($rule);
55 4
        if ($remote) {
56 1
            list($attribute, $parameters) = $this->remoteRule($attribute, $forceRemote);
57 1
            $jsRule = self::REMOTE_RULE;
58
        } else {
59 3
            list($jsRule, $attribute, $parameters) = $this->clientRule($attribute, $rule, $parameters);
60
        }
61
62 4
        $attribute = $this->getAttributeName($attribute);
63
64 4
        return [$attribute, $jsRule, $parameters];
65
    }
66
67
    /**
68
     * Gets rules.
69
     *
70
     * @return array
71
     */
72 1
    public function getValidatorRules()
73
    {
74 1
        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 3
    protected function clientRule($attribute, $rule, $parameters)
87
    {
88 3
        $jsRule = self::JAVASCRIPT_RULE;
89 3
        $method = "rule{$rule}";
90
91 3
        if (method_exists($this, $method)) {
92 1
            list($attribute, $parameters) = $this->$method($attribute, $parameters);
93
        }
94
95 3
        return [$jsRule, $attribute, $parameters];
96
    }
97
98
    /**
99
     * Returns Javascript parameters for remote validated rules.
100
     *
101
     * @param string $attribute
102
     * @param bool $forceRemote
103
     *
104
     * @return array
105
     */
106 1
    protected function remoteRule($attribute, $forceRemote)
107
    {
108
        $params = [
109 1
            $attribute,
110 1
            $this->remoteToken,
111 1
            $forceRemote,
112
        ];
113
114 1
        return [$attribute, $params];
115
    }
116
117
    /**
118
     * Handles multidimensional attribute names.
119
     *
120
     * @param $attribute
121
     *
122
     * @return string
123
     */
124 4
    protected function getAttributeName($attribute)
125
    {
126 4
        $attributeArray = explode('.', $attribute);
127 4
        if (count($attributeArray) > 1) {
128 1
            return $attributeArray[0].'['.implode('][', array_slice($attributeArray, 1)).']';
129
        }
130
131 3
        return $attribute;
132
    }
133
}
134