Completed
Push — master ( 3eb68a...8710c4 )
by
unknown
02:30
created

RuleListTrait::isFormRequestRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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