Completed
Push — master ( 622505...45c85a )
by Albert
9s
created

JavascriptRulesTrait::ruleDistinct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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