Completed
Push — master ( 33cdc3...038af3 )
by Lorenzo
01:59
created

validation.php ➔ isIntegerFloatingPoint()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 4
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 8
rs 8.8571
1
<?php
2
3
/**
4
 * Check if a string number starts with one ore more zero
5
 * i.e.: 00...000 or 000...0Xxxx.x  with X an int
6
 * @param $value
7
 * @return bool
8
 */
9
function isStringNumberStartsWithMoreThanOneZero($value)
10
{
11
    return preg_match('/^[0]{2,}$/', $value) === 1 || preg_match('/^0{1,}[1-9]{1,}$/', $value) === 1;
12
}
13
14
/**
15
 * Check if the value (int, float or string) is a integer.
16
 * Only number <=PHP_INT_MAX (and >=PHP_INT_MIN if unsigned=true)
17
 * or if $acceptIntegerFloatingPoints==true a floating point that match an integer).
18
 * @param $value
19
 * @param bool $unsigned
20
 * @param bool $acceptIntegerFloatingPoints
21
 * @return bool
22
 */
23
function isInteger($value, $unsigned = true, $acceptIntegerFloatingPoints = false) : bool
24
{
25
    if (isStringNumberStartsWithMoreThanOneZero($value)) {
26
        return false;
27
    }
28
29
    //accept only integer number and if $acceptIntegerFloatingPoints is true accept integer floating point too.
30
    return ((preg_match('/^' . ($unsigned ? '' : '-{0,1}') . '[0-9]{1,}$/', $value) === 1
31
            && ($value <= PHP_INT_MAX && $value >= PHP_INT_MIN && (((int)$value) == $value))
32
        )
33
        || ($acceptIntegerFloatingPoints && isIntegerFloatingPoint($value, $unsigned)));
34
}
35
36
/**
37
 * Check if string is a valid floating point that
38
 * match an integer (<=PHP_INT_MAX and >=PHP_INT_MIN if unsigned=true)
39
 * or is an integer
40
 * Ex.: 1, 1e2, 1E2, 1e+2, 1e-2, 1.4e+2, -1.2e+2, -1.231e-2 etc...
41
 * @param $value
42
 * @param bool $unsigned
43
 * @return bool
44
 */
45
function isIntegerFloatingPoint($value, $unsigned = true) : bool
46
{
47
    return isFloatingPoint($value, $unsigned)
48
    && $value <= PHP_INT_MAX && $value >= PHP_INT_MIN
49
    //big number rouned to int aproximately!
50
    //big number change into exp format
51
    && ((int)((double)$value) == $value || (int)$value == $value || strpos(strtoupper((string)$value), 'E') === false);
52
}
53
54
/**
55
 * Check if string is a valid floating point.
56
 * Ex.: 1, 1e2, 1E2, 1e+2, 1e-2, 1.43234e+2, -1.231e+2, -1.231e-2 etc...
57
 * @param $value
58
 * @param $unsigned
59
 * @return bool
60
 */
61
function isFloatingPoint($value, $unsigned) : bool
62
{
63
    if (isStringNumberStartsWithMoreThanOneZero($value)) {
64
        return false;
65
    }
66
67
    return preg_match('/^' . ($unsigned ? '' : '-{0,1}') . '[0-9]{1,}(\.[0-9]{1,}){0,1}([Ee][+,-]{0,1}[0-9]{1,}){0,}$/',
68
        $value) === 1;
69
}
70
71
/**
72
 * Check if the value are a double (integer or float in the form 1, 1.11...1.
73
 * @param $value
74
 * @param int $dec
75
 * @param bool $unsigned
76
 * @param bool $exactDec if set to true aspect number of dec exact to $dec,
77
 * otherwise $dec is max decimals accepted (0 decimals are also ok in this case).
78
 * if $dec is an empty string, accept 0 to infinite decimals.
79
 * @return bool
80
 */
81
function isDouble($value, $dec = 2, $unsigned = true, $exactDec = false) : bool
82
{
83
    if (isStringNumberStartsWithMoreThanOneZero($value)) {
84
        return false;
85
    }
86
    $regEx = '/^' . ($unsigned ? '' : '-{0,1}') . '[0-9]{1,}(\.{1}[0-9]{' . ($exactDec ? '' : '1,') . $dec . '})' . ($exactDec ? '{1}' : '{0,1}') . '$/';
87
    return preg_match($regEx, $value) === 1;
88
}
89
90
/**
91
 * Check if string is dd/mm/YYYY
92
 * @param $value
93
 * @return bool
94
 */
95 View Code Duplication
function isDateIta($value) : bool
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
96
{
97
    if ($value === null || $value == '' || strlen($value) != 10 || strpos($value, '/') === false) {
98
        return false;
99
    }
100
    list($dd, $mm, $yyyy) = explode('/', $value);
101
    try {
102
        return checkdate($mm, $dd, $yyyy);
103
    } catch (Exception $e) {
104
        return false;
105
    }
106
}
107
108
/**
109
 * Check if string is YYYY-mm-dd
110
 * @param $value
111
 * @return bool
112
 */
113 View Code Duplication
function isDateIso($value) : bool
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
114
{
115
    if ($value === null || $value == '' || strlen($value) != 10 || strpos($value, '-') === false) {
116
        return false;
117
    }
118
    list($yyyy, $mm, $dd) = explode('-', $value);
119
    try {
120
        return checkdate($mm, $dd, $yyyy);
121
    } catch (Exception $e) {
122
        return false;
123
    }
124
}
125
126
/**
127
 * Check if string is YYYY-mm-dd HH:ii:ss
128
 * @param $value
129
 * @return bool
130
 */
131
function isDateTimeIso($value) : bool
132
{
133
    if (!isDateIso(substr($value, 0, 10))) {
134
        return false;
135
    }
136
    return isTimeIso(substr($value, 11));
137
}
138
139
/**
140
 * Check if string is dd/mm/YYYY HH:ii:ss
141
 * @param $value
142
 * @return bool
143
 */
144
function isDateTimeIta($value) : bool
145
{
146
    if (!isDateIta(substr($value, 0, 10))) {
147
        return false;
148
    }
149
    return isTimeIso(substr($value, 11));
150
}
151
152
/**
153
 * Check if string is HH:ii:ss
154
 * @param $value
155
 * @return bool
156
 */
157
function isTimeIso($value) : bool
158
{
159
    $strRegExp = '/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/';
160
    return preg_match($strRegExp, $value) === 1;
161
}
162
163
/**
164
 * An alias of isTimeIso.
165
 * @param $value
166
 * @return bool
167
 */
168
function isTimeIta($value)
169
{
170
    return isTimeIso($value);
171
}
172
173
/**
174
 * @param $value
175
 * @return bool
176
 */
177
function isMail($value) : bool
178
{
179
    return !(filter_var($value, FILTER_VALIDATE_EMAIL) === false);
180
}
181
182
/**
183
 * isIPv4 check if is a valid IP v4
184
 * @param  string $IP2Check IP to check
185
 * @return bool
186
 */
187
function isIPv4($IP2Check) : bool
188
{
189
    return !(filter_var($IP2Check, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false);
190
}
191
192
/**
193
 * Simple Check if a URL address syntax is valid (this regular expression also allows dashes in the URL).
194
 * Do not support ful URI and allow only popular scheme (http|https|ftp|mailto|file|data).
195
 * Require scheme protocol or www. i.e.: http://dummy.com and www.dummy.com return true but dummy.com return false.
196
 * @param $url
197
 * @return bool
198
 */
199
function isUrl($url) : bool
200
{
201
    if (preg_match('/^(https?|ftp|mailto|file|data):\/\/\./i', $url) === 1) {
202
        return false;
203
    }
204
    return preg_match('/\b(?:(?:https?|ftp|mailto|file|data):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i',
205
        $url) === 1;
206
}
207
208
/**
209
 * Controlla partita IVA.
210
 * @author Umberto Salsi <[email protected]>
211
 * @version 2012-05-12
212
 * @param string $pi Partita IVA costituita da 11 cifre. Non sono ammessi
213
 * caratteri di spazio, per cui i campi di input dell'utente dovrebbero
214
 * essere trimmati preventivamente. La stringa vuota e' ammessa, cioe'
215
 * il dato viene considerato opzionale.
216
 * @return bool
217
 */
218
function isPiva(string $pi) : bool
219
{
220
    if ($pi === null || $pi === '' || strlen($pi) != 11 || preg_match("/^[0-9]+\$/", $pi) != 1) {
221
        return false;
222
    }
223
    $s = 0;
224
    for ($i = 0; $i <= 9; $i += 2) {
225
        $s += ord($pi[$i]) - ord('0');
226
    }
227
    for ($i = 1; $i <= 9; $i += 2) {
228
        $c = 2 * (ord($pi[$i]) - ord('0'));
229
        if ($c > 9) {
230
            $c -= 9;
231
        }
232
        $s += $c;
233
    }
234
    return !((10 - $s % 10) % 10 != ord($pi[10]) - ord('0'));
235
}
236
237
/**
238
 * Controlla codice fiscale.
239
 * @author Umberto Salsi <[email protected]>
240
 * @version 2012-05-12
241
 * @param string $cf Codice fiscale costituito da 16 caratteri. Non
242
 * sono ammessi caratteri di spazio, per cui i campi di input dell'utente
243
 * dovrebbero essere trimmati preventivamente. La stringa vuota e' ammessa,
244
 * cioe' il dato viene considerato opzionale.
245
 * @return bool
246
 */
247
function isCf(string $cf) : bool
248
{
249
    if ($cf === null || $cf === '' || strlen($cf) != 16) {
250
        return false;
251
    }
252
    $cf = strtoupper($cf);
253
    if (preg_match("/^[A-Z0-9]+\$/", $cf) != 1) {
254
        return false;
255
    }
256
    $s = 0;
257
    for ($i = 1; $i <= 13; $i += 2) {
258
        $c = $cf[$i];
259
        if (strcmp($c, "0") >= 0 && strcmp($c, "9") <= 0) {
260
            $s += ord($c) - ord('0');
261
        } else {
262
            $s += ord($c) - ord('A');
263
        }
264
    }
265
    for ($i = 0; $i <= 14; $i += 2) {
266
        $c = $cf[$i];
267
        switch ($c) {
268
            case '0':
269
                $s += 1;
270
                break;
271
            case '1':
272
                $s += 0;
273
                break;
274
            case '2':
275
                $s += 5;
276
                break;
277
            case '3':
278
                $s += 7;
279
                break;
280
            case '4':
281
                $s += 9;
282
                break;
283
            case '5':
284
                $s += 13;
285
                break;
286
            case '6':
287
                $s += 15;
288
                break;
289
            case '7':
290
                $s += 17;
291
                break;
292
            case '8':
293
                $s += 19;
294
                break;
295
            case '9':
296
                $s += 21;
297
                break;
298
            case 'A':
299
                $s += 1;
300
                break;
301
            case 'B':
302
                $s += 0;
303
                break;
304
            case 'C':
305
                $s += 5;
306
                break;
307
            case 'D':
308
                $s += 7;
309
                break;
310
            case 'E':
311
                $s += 9;
312
                break;
313
            case 'F':
314
                $s += 13;
315
                break;
316
            case 'G':
317
                $s += 15;
318
                break;
319
            case 'H':
320
                $s += 17;
321
                break;
322
            case 'I':
323
                $s += 19;
324
                break;
325
            case 'J':
326
                $s += 21;
327
                break;
328
            case 'K':
329
                $s += 2;
330
                break;
331
            case 'L':
332
                $s += 4;
333
                break;
334
            case 'M':
335
                $s += 18;
336
                break;
337
            case 'N':
338
                $s += 20;
339
                break;
340
            case 'O':
341
                $s += 11;
342
                break;
343
            case 'P':
344
                $s += 3;
345
                break;
346
            case 'Q':
347
                $s += 6;
348
                break;
349
            case 'R':
350
                $s += 8;
351
                break;
352
            case 'S':
353
                $s += 12;
354
                break;
355
            case 'T':
356
                $s += 14;
357
                break;
358
            case 'U':
359
                $s += 16;
360
                break;
361
            case 'V':
362
                $s += 10;
363
                break;
364
            case 'W':
365
                $s += 22;
366
                break;
367
            case 'X':
368
                $s += 25;
369
                break;
370
            case 'Y':
371
                $s += 24;
372
                break;
373
            case 'Z':
374
                $s += 23;
375
                break;
376
            /*. missing_default: .*/
377
        }
378
    }
379
    return !(chr($s % 26 + ord('A')) != $cf[15]);
380
}
381