Completed
Push — master ( 87a075...2a291a )
by Albert
37:13 queued 33:30
created

JavascriptRulesTrait::ruleRequiredWithoutAll()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 1
    protected function ruleConfirmed($attribute, array $parameters)
33
    {
34 1
        $parameters[0] = $this->getAttributeName($attribute);
35 1
        $attribute = "{$attribute}_confirmation";
36
37 1
        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 4
    protected function ruleAfter($attribute, array $parameters)
49
    {
50 4
        if (! ($date = strtotime($parameters[0]))) {
51 2
            $date = $this->getAttributeName($parameters[0]);
52 2
        }
53
54 4
        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 2
    protected function ruleBefore($attribute, array $parameters)
66
    {
67 2
        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 2
    protected function ruleSame($attribute, array $parameters)
79
    {
80 2
        $other = $this->getAttributeName($parameters[0]);
81
82 2
        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 1
    protected function ruleDifferent($attribute, array $parameters)
94
    {
95 1
        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 4
    protected function ruleRequiredWith($attribute, array $parameters)
107
    {
108 4
        $parameters = array_map([$this, 'getAttributeName'], $parameters);
109
110 4
        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 1
    protected function ruleRequiredWithAll($attribute, array $parameters)
122
    {
123 1
        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 1
    protected function ruleRequiredWithout($attribute, array $parameters)
135
    {
136 1
        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 1
    protected function ruleRequiredWithoutAll($attribute, array $parameters)
148
    {
149 1
        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 3
    protected function ruleRequiredIf($attribute, array $parameters)
161
    {
162 3
        $parameters[0] = $this->getAttributeName($parameters[0]);
163
164 3
        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 1
    protected function ruleRequiredUnless($attribute, array $parameters)
176
    {
177 1
        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
     *
186
     * @return array
187
     */
188 1
    protected function ruleDimensions($attribute, $parameters)
189
    {
190 1
        $parameters = $this->parseNamedParameters($parameters);
191
192 1
        return [$attribute, $parameters];
193
    }
194
}
195