-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMValidator.php
145 lines (123 loc) · 3.66 KB
/
KMValidator.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @author kofimokome
*/
if ( ! class_exists( 'KMValidator' ) ) {
#[AllowDynamicProperties]
class KMValidator {
private $rules = [];
private $data = [];
public function __call( $method, $arguments ) {
if ( $method == 'validate' ) {
return $this->validateData( ...$arguments );
} else {
return $this->$method( ...$arguments );
}
}
public static function __callStatic( $method, $arguments ) {
$instance = new KMValidator();
switch($method){
case 'make':
return $instance->makeRules( ...$arguments );
break;
case 'validate':
return $instance->validateData( ...$arguments );
break;
default:
return $instance->$method( ...$arguments );
break;
}
}
/**
* @since 1.0.0
* Creates a validator request
* Rules: required, bool, int, numeric, pdf
* @author kofimokome
*/
private function makeRules( $rules, $data ): KMValidator {
$this->rules = $rules;
$this->data = $data;
return $this;
}
private function isFile( $data ): bool {
if ( is_array( $data ) && isset( $data['type'] ) && isset( $data['name'] ) && isset( $data['size'] ) && isset( $data['tmp_name'] ) ) {
return true;
}
return false;
}
/**
* @since 1.0.0
* Validates request
* Rules: required, bool, int, numeric, pdf
* @author kofimokome
*/
public function validateData( $rules = [], $data = [] ): bool|array {
if ( sizeof( $rules ) == 0 ) {
$rules = $this->rules;
}
if ( sizeof( $data ) == 0 ) {
$data = $this->data;
}
foreach ( $rules as $field => $rule ) {
$rules_to_check = explode( '|', $rule );
$rules_to_check = array_filter( $rules_to_check, function ( $a ) {
return strlen( $a ) > 0;
} );
foreach ( $rules_to_check as $rule_to_check ) {
switch ( $rule_to_check ) {
case 'required':
if ( ! isset( $data[ $field ] ) || ( isset( $data[ $field ] ) && ( ( ! is_array( $data[ $field ] ) && trim( $data[ $field ] ) == '' ) || ( is_array( $data[ $field ] ) && sizeof( $data[ $field ] ) == 0 ) ) ) ) {
wp_send_json_error( $field . ' is required', 400 );
return false;
}
// $data[ $field ] = trim( $data[ $field ] );
break;
case 'int':
if ( isset( $data[ $field ] ) && ! is_integer( $data[ $field ] ) ) {
wp_send_json_error( $field . ' must be an integer', 400 );
return false;
}
// $data[ $field ] = intval( $data[ $field ] );
break;
case 'numeric':
if ( isset( $data[ $field ] ) && ! is_numeric( $data[ $field ] ) ) {
wp_send_json_error( $field . ' must be a numeric value', 400 );
return false;
}
break;
case 'bool':
if ( isset( $data[ $field ] ) && ( ! is_bool( $data[ $field ] ) && ( $data[ $field ] != 'true' && $data[ $field ] != 'false' ) ) ) {
wp_send_json_error( $field . ' must be a boolean value', 400 );
return false;
}
/*if ( $data[ $field ] == 'true' ) {
$data[ $field ] = 1;
} elseif ( $data[ $field ] == 'false' ) {
$data[ $field ] = 0;
} else {
$data[ $field ] = (bool) $data[ $field ];
}*/
break;
case 'pdf':
if ( isset( $data[ $field ] ) ) {
if ( self::isFile( $data[ $field ] ) ) {
if ( $data[ $field ]['type'] != 'application/pdf' ) {
wp_send_json_error( $field . ' must be a pdf file', 400 );
return false;
}
} else {
wp_send_json_error( $field . ' must be a file', 400 );
return false;
}
}
break;
}
}
}
// return $data;
$this->rules = [];
$this->data = [];
return $data;
}
}
}