1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PluginSimpleValidate\helper\Validate; |
4
|
|
|
|
5
|
|
|
use libphonenumber\NumberParseException; |
6
|
|
|
use libphonenumber\PhoneNumber; |
7
|
|
|
use libphonenumber\PhoneNumberUtil; |
8
|
|
|
use PluginSimpleValidate\BaseAbstract\Field; |
9
|
|
|
use PluginSimpleValidate\Exception\InvalidTypeParameter; |
10
|
|
|
use function PluginSimpleValidate\helper\Cleaner\get_length; |
11
|
|
|
use function PluginSimpleValidate\helper\Cleaner\is_valid_type_for_length; |
12
|
|
|
use function PluginSimpleValidate\helper\Cleaner\trim_doubled_space; |
13
|
|
|
|
14
|
|
|
if (! function_exists('is_true')) { |
15
|
|
|
function is_true($value) |
16
|
|
|
{ |
17
|
1 |
|
return $value === true; |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (! function_exists('is_number')) { |
22
|
|
|
function is_number($value) |
23
|
|
|
{ |
24
|
|
|
return is_numeric($value); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (! function_exists('is_required')) { |
29
|
|
|
function is_required($value) |
30
|
|
|
{ |
31
|
4 |
|
return !empty(trim_doubled_space($value)); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (! function_exists('is_valid_email')) { |
36
|
|
|
function is_valid_email($value) |
37
|
|
|
{ |
38
|
3 |
|
return preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $value); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (! function_exists('is_alpha')) { |
43
|
|
|
function is_alpha($value) |
44
|
|
|
{ |
45
|
|
|
return preg_match("/^([a-z])+$/i", $value); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (! function_exists('is_alpha_or_numeric')) { |
50
|
|
|
function is_alpha_or_numeric($value) |
51
|
|
|
{ |
52
|
1 |
|
return preg_match("/^([a-z0-9])+$/i", $value); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (! function_exists('is_decimal')) { |
57
|
|
|
function is_decimal($value) |
58
|
|
|
{ |
59
|
|
|
return preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $value); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! function_exists('is_integer')) { |
64
|
|
|
function is_integer($value) |
65
|
|
|
{ |
66
|
|
|
return preg_match('/^[\-+]?[0-9]+$/', $value); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (! function_exists('is_natural')) { |
71
|
|
|
function is_natural($value) |
72
|
|
|
{ |
73
|
|
|
return preg_match('/^[0-9]+$/', $value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (! function_exists('is_natural_no_zero')) { |
78
|
|
|
function is_natural_no_zero($value) |
79
|
|
|
{ |
80
|
|
|
return preg_match('/^[1-9]+[0]*$/', $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (! function_exists('is_equal')) { |
85
|
|
|
function is_equal($value, array $args) |
86
|
|
|
{ |
87
|
1 |
|
return $value === $args[Field::VAR_MATCH]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (! function_exists('less_than')) { |
92
|
|
|
function less_than($value, array $args) |
93
|
|
|
{ |
94
|
1 |
|
return $value < $args[Field::VAR_LIMIT]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (! function_exists('greater_than')) { |
99
|
|
|
function greater_than($value, array $args) |
100
|
|
|
{ |
101
|
|
|
return $value > $args[Field::VAR_LIMIT]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (! function_exists('less_or_equal_than')) { |
106
|
|
|
function less_or_equal_than($value, array $args) |
107
|
|
|
{ |
108
|
|
|
return $value <= $args[Field::VAR_LIMIT]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (! function_exists('greater_or_equal_than')) { |
113
|
|
|
function greater_or_equal_than($value, array $args) |
114
|
|
|
{ |
115
|
|
|
return $value >= $args[Field::VAR_LIMIT]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (! function_exists('between')) { |
120
|
|
|
function between($value, array $args) |
121
|
|
|
{ |
122
|
1 |
|
return $value < $args[Field::VAR_UPPER_LIMIT] && $value > $args[Field::VAR_LOWER_LIMIT]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (! function_exists('between_or_equal')) { |
127
|
|
|
function between_or_equal($value, array $args) |
128
|
|
|
{ |
129
|
|
|
return $value <= $args[Field::VAR_UPPER_LIMIT] && $value >= $args[Field::VAR_LOWER_LIMIT]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
if (! function_exists('length')) { |
|
|
|
|
134
|
|
|
function length($value, array $args) |
135
|
|
|
{ |
136
|
|
|
return run_length_rule( |
137
|
|
|
$value, |
138
|
|
|
get_length($value) === $args[Field::VAR_LIMIT] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
if (! function_exists('length_less_than')) { |
|
|
|
|
144
|
|
|
function length_less_than($value, array $args) |
145
|
|
|
{ |
146
|
|
|
return run_length_rule( |
147
|
|
|
$value, |
148
|
|
|
get_length($value) < $args[Field::VAR_LIMIT] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
if (! function_exists('length_greater_than')) { |
|
|
|
|
154
|
|
|
function length_greater_than($value, array $args) |
155
|
|
|
{ |
156
|
1 |
|
return run_length_rule( |
157
|
1 |
|
$value, |
158
|
1 |
|
get_length($value) > $args[Field::VAR_LIMIT] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
if (! function_exists('length_less_or_equal_than')) { |
|
|
|
|
164
|
|
|
function length_less_or_equal_than($value, array $args) |
165
|
|
|
{ |
166
|
|
|
return run_length_rule( |
167
|
|
|
$value, |
168
|
|
|
get_length($value) <= $args[Field::VAR_LIMIT] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
View Code Duplication |
if (! function_exists('length_greater_or_equal_than')) { |
|
|
|
|
174
|
|
|
function length_greater_or_equal_than($value, array $args) |
175
|
|
|
{ |
176
|
|
|
return run_length_rule( |
177
|
|
|
$value, |
178
|
|
|
get_length($value) >= $args[Field::VAR_LIMIT] |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
if (! function_exists('length_between')) { |
|
|
|
|
184
|
|
|
function length_between($value, array $args) |
185
|
|
|
{ |
186
|
|
|
return run_length_rule( |
187
|
|
|
$value, |
188
|
|
|
get_length($value) < $args[Field::VAR_UPPER_LIMIT] && get_length($value) > $args[Field::VAR_LOWER_LIMIT] |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
View Code Duplication |
if (! function_exists('length_between_or_equal')) { |
|
|
|
|
195
|
|
|
function length_between_or_equal($value, array $args) |
196
|
|
|
{ |
197
|
|
|
return run_length_rule( |
198
|
|
|
$value, |
199
|
|
|
get_length($value) <= $args[Field::VAR_UPPER_LIMIT] && get_length($value) >= $args[Field::VAR_LOWER_LIMIT] |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (! function_exists('valid_phone_number')) { |
205
|
|
|
function valid_phone_number($value, array $args) |
206
|
|
|
{ |
207
|
|
|
try { |
208
|
|
|
/** @var PhoneNumber $phoneNumber */ |
209
|
|
|
$phoneNumber = PhoneNumberUtil::getInstance()->parse($value, $args[Field::VAR_REGION]); |
210
|
|
|
return PhoneNumberUtil::getInstance()->isValidNumberForRegion( |
211
|
|
|
$phoneNumber, |
212
|
|
|
$args[Field::VAR_REGION] |
213
|
|
|
); |
214
|
|
|
} catch (NumberParseException $exception) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (! function_exists('run_length_rule')) { |
221
|
|
|
function run_length_rule($value, $result) { |
222
|
1 |
|
if (!is_valid_type_for_length($value)) { |
223
|
|
|
throw new InvalidTypeParameter('Invalid parameter type'); |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
return $result; |
227
|
|
|
} |
228
|
|
|
} |
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.