Completed
Push — master ( bc0514...a15442 )
by
unknown
01:52
created

Validate.php ➔ run_length_rule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PluginSimpleValidate\helper\Validate;
4
5
use PluginSimpleValidate\BaseAbstract\Field;
6
use PluginSimpleValidate\Exception\InvalidTypeParameter;
7
use function PluginSimpleValidate\helper\Cleaner\get_length;
8
use function PluginSimpleValidate\helper\Cleaner\is_valid_type_for_length;
9
use function PluginSimpleValidate\helper\Cleaner\trim_doubled_space;
10
11
if (! function_exists('is_true')) {
12
    function is_true($value)
13
    {
14 1
        return $value === true;
15
    }
16
}
17
18
if (! function_exists('is_number')) {
19
    function is_number($value)
20
    {
21
        return is_numeric($value);
22
    }
23
}
24
25
if (! function_exists('is_required')) {
26
    function is_required($value)
27
    {
28 4
        return !empty(trim_doubled_space($value));
29
    }
30
}
31
32
if (! function_exists('is_valid_email')) {
33
    function is_valid_email($value)
34
    {
35 3
        return preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $value);
36
    }
37
}
38
39
if (! function_exists('is_alpha')) {
40
    function is_alpha($value)
41
    {
42
        return preg_match("/^([a-z])+$/i", $value);
43
    }
44
}
45
46
if (! function_exists('is_alpha_or_numeric')) {
47
    function is_alpha_or_numeric($value)
48
    {
49 1
        return preg_match("/^([a-z0-9])+$/i", $value);
50
    }
51
}
52
53
if (! function_exists('is_decimal')) {
54
    function is_decimal($value)
55
    {
56
        return preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $value);
57
    }
58
}
59
60
if (! function_exists('is_integer')) {
61
    function is_integer($value)
62
    {
63
        return preg_match('/^[\-+]?[0-9]+$/', $value);
64
    }
65
}
66
67
if (! function_exists('is_natural')) {
68
    function is_natural($value)
69
    {
70
        return preg_match('/^[0-9]+$/', $value);
71
    }
72
}
73
74
if (! function_exists('is_natural_no_zero')) {
75
    function is_natural_no_zero($value)
76
    {
77
        return preg_match('/^[1-9]+[0]*$/', $value);
78
    }
79
}
80
81
if (! function_exists('is_equal')) {
82
    function is_equal($value, array $args)
83
    {
84 1
        return $value === $args[Field::VAR_MATCH];
85
    }
86
}
87
88
if (! function_exists('less_than')) {
89
    function less_than($value, array $args)
90
    {
91 1
        return $value < $args[Field::VAR_LIMIT];
92
    }
93
}
94
95
if (! function_exists('greater_than')) {
96
    function greater_than($value, array $args)
97
    {
98
        return $value > $args[Field::VAR_LIMIT];
99
    }
100
}
101
102
if (! function_exists('less_or_equal_than')) {
103
    function less_or_equal_than($value, array $args)
104
    {
105
        return $value <= $args[Field::VAR_LIMIT];
106
    }
107
}
108
109
if (! function_exists('greater_or_equal_than')) {
110
    function greater_or_equal_than($value, array $args)
111
    {
112
        return $value >= $args[Field::VAR_LIMIT];
113
    }
114
}
115
116
if (! function_exists('between')) {
117
    function between($value, array $args)
118
    {
119 1
        return $value < $args[Field::VAR_UPPER_LIMIT] && $value > $args[Field::VAR_LOWER_LIMIT];
120
    }
121
}
122
123
if (! function_exists('between_or_equal')) {
124
    function between_or_equal($value, array $args)
125
    {
126
        return $value <= $args[Field::VAR_UPPER_LIMIT] && $value >= $args[Field::VAR_LOWER_LIMIT];
127
    }
128
}
129
130 View Code Duplication
if (! function_exists('length')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    function length($value, array $args)
132
    {
133
        return run_length_rule(
134
            $value,
135
            get_length($value) === $args[Field::VAR_LIMIT]
136
        );
137
    }
138
}
139
140 View Code Duplication
if (! function_exists('length_less_than')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    function length_less_than($value, array $args)
142
    {
143
        return run_length_rule(
144
            $value,
145
            get_length($value) < $args[Field::VAR_LIMIT]
146
        );
147
    }
148
}
149
150 View Code Duplication
if (! function_exists('length_greater_than')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    function length_greater_than($value, array $args)
152
    {
153 1
        return run_length_rule(
154 1
            $value,
155 1
            get_length($value) > $args[Field::VAR_LIMIT]
156
        );
157
    }
158
}
159
160 View Code Duplication
if (! function_exists('length_less_or_equal_than')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    function length_less_or_equal_than($value, array $args)
162
    {
163
        return run_length_rule(
164
            $value,
165
            get_length($value) <= $args[Field::VAR_LIMIT]
166
        );
167
    }
168
}
169
170 View Code Duplication
if (! function_exists('length_greater_or_equal_than')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    function length_greater_or_equal_than($value, array $args)
172
    {
173
        return run_length_rule(
174
            $value,
175
            get_length($value) >= $args[Field::VAR_LIMIT]
176
        );
177
    }
178
}
179
180 View Code Duplication
if (! function_exists('length_between')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    function length_between($value, array $args)
182
    {
183
        return run_length_rule(
184
            $value,
185
            get_length($value) < $args[Field::VAR_UPPER_LIMIT] && get_length($value) > $args[Field::VAR_LOWER_LIMIT]
186
        );
187
    }
188
}
189
190
191 View Code Duplication
if (! function_exists('length_between_or_equal')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    function length_between_or_equal($value, array $args)
193
    {
194
        return run_length_rule(
195
            $value,
196
            get_length($value) <= $args[Field::VAR_UPPER_LIMIT] && get_length($value) >= $args[Field::VAR_LOWER_LIMIT]
197
        );
198
    }
199
}
200
201
if (! function_exists('run_length_rule')) {
202
    function run_length_rule($value, $result) {
203 1
        if (!is_valid_type_for_length($value)) {
204
            throw new InvalidTypeParameter('Invalid parameter type');
205
        }
206
207 1
        return $result;
208
    }
209
}