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

RuleListTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 3
cbo 0
dl 0
loc 94
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isImplemented() 0 4 2
A isRemoteRule() 0 5 2
A isDisableRule() 0 4 1
A validationDisabled() 0 6 1
A isFileRule() 0 4 1
1
<?php
2
3
namespace Isrenato\JsValidation\Support;
4
5
trait RuleListTrait
6
{
7
    /**
8
     *  Rules validated with Javascript.
9
     *
10
     * @var array
11
     */
12
    protected $clientRules = ['Accepted', 'After', 'Alpha', 'AlphaDash',
13
        'AlphaNum', 'Array', 'Bail', 'Before', 'Between', 'Boolean', 'Confirmed', 'Date', 'Dimensions',
14
        'DateFormat', 'Different', 'Digits', 'DigitsBetween', 'Distinct', 'Email', 'File', 'Filled', 'Image',
15
        'In', 'InArray', 'Integer', 'Ip', 'Json', 'Max', 'Mimes', 'Mimetypes', 'Min', 'NotIn', 'Nullable',
16
        'Numeric', 'Regex', 'Required', 'RequiredIf', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll',
17
        'RequiredWithout', 'RequiredWithoutAll', 'Same', 'Size', 'Sometimes',
18
        'String', 'Timezone', 'Url', ];
19
20
    /**
21
     * Rules validated in Server-Side.
22
     *
23
     * @var array
24
     */
25
    protected $serverRules = ['ActiveUrl', 'Exists', 'Unique'];
26
27
    /**
28
     * Rules applyed to files.
29
     *
30
     * @var array
31
     */
32
    protected $fileRules = ['File', 'Image', 'Mimes', 'Mimetypes'];
33
34
    /**
35
     * Rule used to disable validations.
36
     *
37
     * @var string
38
     */
39
    private $disableJsValidationRule = 'NoJsValidation';
40
41
    /**
42
     * Returns if rule is validated using Javascript.
43
     *
44
     * @param $rule
45
     * @return bool
46
     */
47
    protected function isImplemented($rule)
48 12
    {
49
        return in_array($rule, $this->clientRules) || in_array($rule, $this->serverRules);
50 12
    }
51
52
    /**
53
     * Check if rule must be validated in server-side.
54
     *
55
     * @param $rule
56
     * @return bool
57
     */
58
    protected function isRemoteRule($rule)
59
    {
60 120
        return in_array($rule, $this->serverRules) ||
61
            ! in_array($rule, $this->clientRules);
62 120
    }
63 120
64
    /**
65
     * Check if rule disables rule processing.
66
     *
67
     * @param $rule
68
     * @return bool
69
     */
70
    protected function isDisableRule($rule)
71
    {
72
        return $rule === $this->disableJsValidationRule;
73 12
    }
74
75 12
    /**
76
     * Check if rules should be validated.
77
     *
78
     * @param $rules
79
     * @return bool
80
     */
81
    protected function validationDisabled($rules)
82
    {
83
        $rules = (array) $rules;
84
85 12
        return in_array($this->disableJsValidationRule, $rules);
86
    }
87 12
88
    /**
89 12
     * Check if rules is for input file type.
90
     *
91
     * @param $rule
92
     * @return bool
93
     */
94
    protected function isFileRule($rule)
95
    {
96
        return in_array($rule, $this->fileRules);
97
    }
98
}
99