|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helpers to manage datas |
|
9
|
|
|
*/ |
|
10
|
|
|
class Datas |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @const ERR_CHECKTYPE_INFOS_FORMAT Exception code if the format of the |
|
14
|
|
|
* infos passed to checkType method is not correct. |
|
15
|
|
|
*/ |
|
16
|
|
|
const ERR_CHECKTYPE_INFOS_FORMAT = 1604001; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @const ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT Exception code if data or |
|
20
|
|
|
* type used to check the variable has not a correct value. |
|
21
|
|
|
*/ |
|
22
|
|
|
const ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT = 1604002; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Check types of variables |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $vars : Variables to check |
|
28
|
|
|
* array(array('type' => 'myType', 'data' => 'myData), array(...)...) |
|
29
|
|
|
* |
|
30
|
|
|
* @return boolean |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function checkType(array $vars): bool |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($vars as $var) { |
|
35
|
|
|
if (!is_array($var)) { |
|
36
|
|
|
throw new Exception( |
|
37
|
|
|
'The informations need for the check is not in a correct format.', |
|
38
|
|
|
self::ERR_CHECKTYPE_INFOS_FORMAT |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ( |
|
43
|
|
|
!isset($var['data']) |
|
44
|
|
|
|| empty($var['type']) |
|
45
|
|
|
|| (isset($var['type']) && !is_string($var['type'])) |
|
46
|
|
|
) { |
|
47
|
|
|
throw new Exception( |
|
48
|
|
|
'Items data or type is empty or in an bad format.', |
|
49
|
|
|
self::ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($var['type'] === 'int') { |
|
54
|
|
|
$var['type'] = 'integer'; |
|
55
|
|
|
} elseif ($var['type'] === 'float') { |
|
56
|
|
|
$var['type'] = 'double'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (gettype($var['data']) !== $var['type']) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check if an email address is valid |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $mail The email address to check |
|
71
|
|
|
* |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function checkMail(string $mail): bool |
|
75
|
|
|
{ |
|
76
|
|
|
$securisedMail = Secure::secureData($mail, 'email', false); |
|
77
|
|
|
|
|
78
|
|
|
if ($securisedMail === false) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|