JavascriptRulesTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 0
dl 0
loc 203
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
getAttributeName() 0 1 ?
parseNamedParameters() 0 1 ?
A ruleAfter() 0 8 2
A ruleInArray() 0 4 1
A ruleDistinct() 0 6 1
A ruleConfirmed() 0 7 1
A ruleBefore() 0 4 1
A ruleSame() 0 6 1
A ruleDifferent() 0 4 1
A ruleRequiredWith() 0 6 1
A ruleRequiredWithAll() 0 4 1
A ruleRequiredWithout() 0 4 1
A ruleRequiredWithoutAll() 0 4 1
A ruleRequiredIf() 0 6 1
A ruleRequiredUnless() 0 4 1
A ruleDimensions() 0 6 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
trait JavascriptRulesTrait
6
{
7
    /**
8
     * Handles multidimensional attribute names.
9
     *
10
     * @param string $attribute
11
     * @return string
12
     */
13
    abstract protected function getAttributeName($attribute);
14
15
    /**
16
     * Parse named parameters to $key => $value items.
17
     *
18
     * @param array $parameters
19
     * @return array
20
     */
21
    abstract public function parseNamedParameters($parameters);
22
23
    /**
24
     * Confirmed rule is applied to confirmed attribute.
25
     *
26
     * @param $attribute
27
     * @param array $parameters
28
     * @return array
29
     */
30 12
    protected function ruleConfirmed($attribute, array $parameters)
31
    {
32 12
        $parameters[0] = $this->getAttributeName($attribute);
33 12
        $attribute = "{$attribute}_confirmation";
34
35 12
        return [$attribute, $parameters];
36
    }
37
38
    /**
39
     * Returns Javascript parameters for After rule.
40
     *
41
     * @param $attribute
42
     * @param array $parameters
43
     * @return array
44
     */
45 48
    protected function ruleAfter($attribute, array $parameters)
46
    {
47 48
        if (! ($date = strtotime($parameters[0]))) {
48 24
            $date = $this->getAttributeName($parameters[0]);
49
        }
50
51 48
        return [$attribute, [$date]];
52
    }
53
54
    /**
55
     * Returns Javascript parameters for Before rule.
56
     *
57
     * @param $attribute
58
     * @param array $parameters
59
     * @return array
60
     */
61 24
    protected function ruleBefore($attribute, array $parameters)
62
    {
63 24
        return $this->ruleAfter($attribute, $parameters);
64
    }
65
66
    /**
67
     * Validate that two attributes match.
68
     *
69
     * @param string $attribute
70
     * @param array $parameters
71
     * @return array
72
     */
73 24
    protected function ruleSame($attribute, array $parameters)
74
    {
75 24
        $other = $this->getAttributeName($parameters[0]);
76
77 24
        return [$attribute, [$other]];
78
    }
79
80
    /**
81
     * Validate that an attribute is different from another attribute.
82
     *
83
     * @param string $attribute
84
     * @param array $parameters
85
     * @return array
86
     */
87 12
    protected function ruleDifferent($attribute, array $parameters)
88
    {
89 12
        return $this->ruleSame($attribute, $parameters);
90
    }
91
92
    /**
93
     * Validate that an attribute exists when any other attribute exists.
94
     *
95
     * @param string $attribute
96
     * @param mixed $parameters
97
     * @return array
98
     */
99 48
    protected function ruleRequiredWith($attribute, array $parameters)
100
    {
101 48
        $parameters = array_map([$this, 'getAttributeName'], $parameters);
102
103 48
        return [$attribute, $parameters];
104
    }
105
106
    /**
107
     * Validate that an attribute exists when all other attributes exists.
108
     *
109
     * @param string $attribute
110
     * @param mixed $parameters
111
     * @return array
112
     */
113 12
    protected function ruleRequiredWithAll($attribute, array $parameters)
114
    {
115 12
        return $this->ruleRequiredWith($attribute, $parameters);
116
    }
117
118
    /**
119
     * Validate that an attribute exists when another attribute does not.
120
     *
121
     * @param string $attribute
122
     * @param mixed $parameters
123
     * @return array
124
     */
125 12
    protected function ruleRequiredWithout($attribute, array $parameters)
126
    {
127 12
        return $this->ruleRequiredWith($attribute, $parameters);
128
    }
129
130
    /**
131
     * Validate that an attribute exists when all other attributes do not.
132
     *
133
     * @param string $attribute
134
     * @param mixed $parameters
135
     * @return array
136
     */
137 12
    protected function ruleRequiredWithoutAll($attribute, array $parameters)
138
    {
139 12
        return $this->ruleRequiredWith($attribute, $parameters);
140
    }
141
142
    /**
143
     * Validate that an attribute exists when another attribute has a given value.
144
     *
145
     * @param string $attribute
146
     * @param mixed $parameters
147
     * @return array
148
     */
149 60
    protected function ruleRequiredIf($attribute, array $parameters)
150
    {
151 60
        $parameters[0] = $this->getAttributeName($parameters[0]);
152
153 60
        return [$attribute, $parameters];
154
    }
155
156
    /**
157
     * Validate that an attribute exists when another attribute does not have a given value.
158
     *
159
     * @param string $attribute
160
     * @param mixed $parameters
161
     * @return array
162
     */
163 12
    protected function ruleRequiredUnless($attribute, array $parameters)
164
    {
165 12
        return $this->ruleRequiredIf($attribute, $parameters);
166
    }
167
168
    /**
169
     * Validate that the values of an attribute is in another attribute.
170
     *
171
     * @param string $attribute
172
     * @param mixed $parameters
173
     * @return array
174
     */
175 12
    protected function ruleInArray($attribute, array $parameters)
176
    {
177 12
        return $this->ruleRequiredIf($attribute, $parameters);
178
    }
179
180
    /**
181
     * Validate the dimensions of an image matches the given values.
182
     *
183
     * @param string $attribute
184
     * @param array $parameters
185
     * @return array
186
     */
187 12
    protected function ruleDimensions($attribute, $parameters)
188
    {
189 12
        $parameters = $this->parseNamedParameters($parameters);
190
191 12
        return [$attribute, $parameters];
192
    }
193
194
    /**
195
     * Validate an attribute is unique among other values.
196
     *
197
     * @param string $attribute
198
     * @param array $parameters
199
     * @return array
200
     */
201 12
    protected function ruleDistinct($attribute, array $parameters)
202
    {
203 12
        $parameters[0] = $attribute;
204
205 12
        return $this->ruleRequiredIf($attribute, $parameters);
206
    }
207
}
208