-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
120 lines (106 loc) · 2.9 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
include "validation.php";
class Testing extends Validation
{
function __construct()
{
Parent::__construct();
}
public function postRequest($request_data)
{
// Here is validation for request parameters are missing or not.
// Convert json request to array by using json_decode function with true option.
$request_data = json_decode($request_data,true);
// Create on array with blank value which are must you want in request parameters.
$requiredRequestFields = array("first_name"=>"","last_name"=>"","age"=>"","email"=>"","contact"=>"","gender"=>"");
// Called a method of Validation Class. That will check a passed json request have all parameters are available or missing any one or more.
$isValid = $this->isValidRequest($requiredRequestFields,$request_data);
// Check is any parameters is missing. If yes then go to else other wise move ahead.
if($isValid=="1"){ // Incomming request have all parameters
// Now here is rules of validations.
/*
* unique value - e.g : If you want to check a email address is already exist in db.
* Then you can use this rule - unique:your_table_name
* e.g : "email":"unique:user_detail"
* That will check a passed email address is already exist in dabase or not.
*/
$rules = array(
"first_name"=>"required",
"last_name"=>"required",
"age"=>"required|integer",
"email"=>"email|unique:user_details",
"contact"=>"required|contactNumber",
"gender"=>"required"
);
$isValid = $this->checkValidation($request_data,$rules);
if($isValid=="1"){
echo "Your request array perfect.<br><pre>";
print_r($request_data);
}else { //
echo $isValid;
}
}else { // Return error message
echo $isValid;
}
}
}
$objTest = new Testing;
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"24",
"email":"xyz@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
/* Testing request
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"",
"email":"xyz@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"Twenty Five",
"email":"xyz@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"25",
"email":"zalasid.mca@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
$request_data = '{
"age":"25",
"email":"xyz.mca@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"24",
"email":"xyz@gmail.com",
"contact":"9876543210",
"gender":"male"
}';
$request_data = '{
"first_name":"Sid",
"last_name":"Jhala",
"age":"24",
"email":"x@y,z@gmail.com",
"contact":"987-654-3210",
"gender":"male"
}';
*/
// Paste testing request_data variable here. :D
$objTest->postRequest($request_data);
?>