Completed
Pull Request — master (#348)
by
unknown
13:10
created

JavascriptRulesTrait::ruleSame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Isrenato\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
    protected function ruleConfirmed($attribute, array $parameters)
31
    {
32 12
        $parameters[0] = $this->getAttributeName($attribute);
33
        $attribute = "{$attribute}_confirmation";
34 12
35 12
        return [$attribute, $parameters];
36
    }
37 12
38
    /**
39
     * Returns Javascript parameters for After rule.
40
     *
41
     * @param $attribute
42
     * @param array $parameters
43
     * @return array
44
     */
45
    protected function ruleAfter($attribute, array $parameters)
46
    {
47
        if (! ($date = strtotime($parameters[0]))) {
48 48
            $date = $this->getAttributeName($parameters[0]);
49
        }
50 48
51 24
        return [$attribute, [$date]];
52 16
    }
53
54 48
    /**
55
     * Returns Javascript parameters for Before rule.
56
     *
57
     * @param $attribute
58
     * @param array $parameters
59
     * @return array
60
     */
61
    protected function ruleBefore($attribute, array $parameters)
62
    {
63
        return $this->ruleAfter($attribute, $parameters);
64
    }
65 24
66
    /**
67 24
     * Validate that two attributes match.
68
     *
69
     * @param string $attribute
70
     * @param array $parameters
71
     * @return array
72
     */
73
    protected function ruleSame($attribute, array $parameters)
74
    {
75
        $other = $this->getAttributeName($parameters[0]);
76
77
        return [$attribute, [$other]];
78 24
    }
79
80 24
    /**
81
     * Validate that an attribute is different from another attribute.
82 24
     *
83
     * @param string $attribute
84
     * @param array $parameters
85
     * @return array
86
     */
87
    protected function ruleDifferent($attribute, array $parameters)
88
    {
89
        return $this->ruleSame($attribute, $parameters);
90
    }
91
92
    /**
93 12
     * Validate that an attribute exists when any other attribute exists.
94
     *
95 12
     * @param string $attribute
96
     * @param mixed $parameters
97
     * @return array
98
     */
99
    protected function ruleRequiredWith($attribute, array $parameters)
100
    {
101
        $parameters = array_map([$this, 'getAttributeName'], $parameters);
102
103
        return [$attribute, $parameters];
104
    }
105
106 48
    /**
107
     * Validate that an attribute exists when all other attributes exists.
108 48
     *
109
     * @param string $attribute
110 48
     * @param mixed $parameters
111
     * @return array
112
     */
113
    protected function ruleRequiredWithAll($attribute, array $parameters)
114
    {
115
        return $this->ruleRequiredWith($attribute, $parameters);
116
    }
117
118
    /**
119
     * Validate that an attribute exists when another attribute does not.
120
     *
121 12
     * @param string $attribute
122
     * @param mixed $parameters
123 12
     * @return array
124
     */
125
    protected function ruleRequiredWithout($attribute, array $parameters)
126
    {
127
        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 12
     * @param mixed $parameters
135
     * @return array
136 12
     */
137
    protected function ruleRequiredWithoutAll($attribute, array $parameters)
138
    {
139
        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 12
     * @return array
148
     */
149 12
    protected function ruleRequiredIf($attribute, array $parameters)
150
    {
151
        $parameters[0] = $this->getAttributeName($parameters[0]);
152
153
        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 60
     * @param mixed $parameters
161
     * @return array
162 60
     */
163
    protected function ruleRequiredUnless($attribute, array $parameters)
164 60
    {
165
        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
    protected function ruleDimensions($attribute, $parameters)
188 12
    {
189
        $parameters = $this->parseNamedParameters($parameters);
190 12
191
        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