-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFvss.php
102 lines (92 loc) · 5.22 KB
/
Fvss.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace CG\FVSS;
class Fvss {
public function validate($data){
$status = true;
$message = null;
foreach($data as $key => $val){
$value = $val["value"];
if(isset($val["length"]) && $val["length"] && !empty($val["length"])){
if(strlen($val["value"]) < $val["length"]){
$status = false;
$message = (isset($val["label"]) ? $val["label"] : ucwords($value)) . " must be at least ". $val["length"] ." characters long";
}
}
if(isset($val["space"]) && $val["space"] && !empty($val["space"])){
$value = preg_replace("/[ \t\n\f]+/", "", $value);
}
if(isset($val["punct"]) && $val["punct"] && !empty($val["punct"])){
$value = preg_replace("#[[:punct:]]#", "", $value);
}
$set_rules = explode("-", $val["type"]);
foreach($set_rules as $rkey => $rval){
switch($rval){
case "alpha" :
if(!ctype_alpha($value)){
$status = false;
$message = "Invalid ". (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. Only Letters allowed";
}
break;
case "num" :
if(!ctype_digit($value)){
$status = false;
$message = "Invalid ". (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. Only Numbers allowed";
}
break;
case "alphanum" :
if(!ctype_alnum($value)){
$status = false;
$message = "Invalid ". (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. Only Letters and Numbers allowed";
}
break;
case "email" :
if(!filter_var($value, FILTER_VALIDATE_EMAIL)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not Email format";
}
break;
case "date" :
if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not Date format";
}
break;
case "datetime" :
if(!preg_match("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/", $value)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not Date Time format";
}
break;
case "time" :
if(!preg_match("/^\d{2}:\d{2}:\d{2}$/", $value)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not Time format";
}
break;
case "url" :
if(!filter_var($value, FILTER_VALIDATE_URL)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not URL format";
}
break;
case "domain" :
if(!preg_match("/^(www\.)?[a-zA-Z0-9][a-zA-Z0-9-]*\.[a-zA-Z]{2,}$/", $value)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not Domain format";
}
break;
case "ip" :
if(!filter_var($value, FILTER_VALIDATE_IP)){
$status = false;
$message = "Invalid " . (isset($val["label"]) ? $val["label"] : ucwords($val["value"])) ." format. not IP format";
}
break;
}
}
}
return (object) [
"status" => $status,
"message" => $message
];
}
}