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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / ValidationHelper.php
tutor / helpers Last commit date
QueryHelper.php 2 years ago SessionHelper.php 3 years ago ValidationHelper.php 2 years ago
ValidationHelper.php
291 lines
1 <?php
2 /**
3 * Form validation helper
4 *
5 * Provides static helper methods for form validation.
6 *
7 * @package Tutor\Helper
8 * @author Themum<support@themeum.com>
9 * @link https://themeum.com
10 * @since 2.6.0
11 */
12
13 namespace Tutor\Helpers;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 return;
17 }
18
19 /**
20 * Validation class contains static methods
21 */
22 class ValidationHelper {
23
24 /**
25 * Validate array elements
26 *
27 * @since 2.6.0
28 *
29 * @param array $validation_rules associative array for validation
30 * rules. For ex: [id => 'required|number', name => 'alpha_numeric|max:255'].
31 *
32 * @param array $data key value pair of data. Note array index should
33 * exactly match with validation_rules array index.
34 *
35 * @return object validation response
36 */
37 public static function validate( array $validation_rules, array $data ): object {
38 $validation_pass = true;
39 $validation_errors = array();
40
41 foreach ( $validation_rules as $key => $validation_rule ) {
42 $rules = explode( '|', $validation_rule );
43
44 foreach ( $rules as $rule ) {
45 $nested_rules = explode( ':', $rule );
46
47 /**
48 * Optional input validation.
49 */
50 if ( isset( $nested_rules[0] ) && 'if_input' === $nested_rules[0] ) {
51 if ( ! self::has_key( $key, $data ) ) {
52 break;
53 }
54 }
55
56 foreach ( $nested_rules as $nested_rule ) {
57 switch ( $nested_rule ) {
58 case 'required':
59 if ( ! self::has_key( $key, $data ) || self::is_empty( $data[ $key ] ) ) {
60 $validation_pass = false;
61 $validation_errors[ $key ][] = $key . __( ' is required', 'tutor' );
62 }
63 break;
64 case 'numeric':
65 if ( ! self::is_numeric( $data[ $key ] ) ) {
66 $validation_pass = false;
67 $validation_errors[ $key ][] = $key . __( ' is not numeric', 'tutor' );
68 }
69 break;
70 case 'min_length':
71 if ( strlen( $data[ $key ] ) < $nested_rules[1] ) {
72 $validation_pass = false;
73 $validation_errors[ $key ][] = $key . __( ' minimum length is ', 'tutor' ) . $nested_rules[1];
74 }
75 break;
76 case 'mimes':
77 $extensions = explode( ',', $nested_rules[1] );
78 if ( ! self::in_array( $data[ $key ], $extensions ) ) {
79 $validation_pass = false;
80 $validation_errors[ $key ][] = $key . __( ' extension is not valid', 'tutor' );
81 }
82 break;
83 case 'match_string':
84 $strings = explode( ',', $nested_rules[1] );
85 if ( ! self::in_array( $data[ $key ], $strings ) ) {
86 $validation_pass = false;
87 $validation_errors[ $key ][] = $key . __( ' string is not valid', 'tutor' );
88 }
89 break;
90 case 'boolean':
91 if ( ! self::is_boolean( $data[ $key ] ) ) {
92 $validation_pass = false;
93 $validation_errors[ $key ][] = $key . __( ' is not boolean', 'tutor' );
94 }
95 break;
96 case 'is_array':
97 if ( ! self::is_array( $data[ $key ] ) ) {
98 $validation_pass = false;
99 $validation_errors[ $key ][] = $key . __( ' is not an array', 'tutor' );
100 }
101 break;
102 case 'date_format':
103 $format = $nested_rules[1];
104 if ( ! self::is_valid_date( $data[ $key ], $format ) ) {
105 $validation_pass = false;
106 $validation_errors[ $key ][] = $key . __( ' invalid date format', 'tutor' );
107 }
108 break;
109
110 case 'has_record':
111 list( $table, $column ) = explode( ',', $nested_rules[1], 2 );
112
113 $value = $data[ $key ];
114 $has_record = self::has_record( $table, $column, $value );
115 if ( ! $has_record ) {
116 $validation_pass = false;
117 $validation_errors[ $key ][] = $key . __( ' record not found', 'tutor' );
118 }
119 break;
120
121 case 'user_exists':
122 $user_id = (int) $data[ $key ];
123 $is_exists = self::is_user_exists( $user_id );
124 if ( ! $is_exists ) {
125 $validation_pass = false;
126 $validation_errors[ $key ][] = $key . __( ' user does not exist', 'tutor' );
127 }
128 break;
129 default:
130 // code...
131 break;
132 }
133 }
134 }
135 }
136
137 $response = array(
138 'success' => $validation_pass,
139 'errors' => $validation_errors,
140 );
141
142 return (object) $response;
143 }
144
145 /**
146 * Check if value is numeric
147 *
148 * Rules: numeric
149 *
150 * @param mixed $value value to check.
151 *
152 * @return boolean
153 */
154 public static function is_numeric( $value ): bool {
155 return is_numeric( $value );
156 }
157
158 /**
159 * Check if value is empty
160 *
161 * Value will be considered empty if it is either null or empty string.
162 *
163 * Rules: required
164 *
165 * @param mixed $value value to check.
166 *
167 * @return boolean
168 */
169 public static function is_empty( $value ): bool {
170 return '' === $value || is_null( $value ) ? true : false;
171 }
172
173 /**
174 * Check if array has key
175 *
176 * @param string $key key to check.
177 * @param array $array_assoc array where to check.
178 *
179 * @return boolean
180 */
181 public static function has_key( string $key, array $array_assoc ): bool {
182 return isset( $array_assoc[ $key ] );
183 }
184
185 /**
186 * Check if element has in array
187 *
188 * Rules: match_string:{value1},{value2}",
189 *
190 * @param string $key key to check.
191 * @param array $array array where to check.
192 *
193 * @return boolean
194 */
195 public static function in_array( string $key, array $array ): bool {
196 return in_array( $key, $array );
197 }
198
199
200 /**
201 * The function checks if a given value is a boolean.
202 *
203 * Considered values: array( 1, 0, 'true', 'false', true, false ), any value
204 * except these will be not counted as boolean
205 *
206 * Rules: boolean
207 *
208 * @param mixed $value is the variable that will be checked if it is a boolean value or not.
209 *
210 * @return bool A boolean value is being returned, indicating whether the input value is a valid
211 * boolean or not.
212 */
213 public static function is_boolean( $value ): bool {
214 $allowed_booleans = array( 1, 0, '1', '0', 'true', 'false', true, false );
215 return in_array( $value, $allowed_booleans, true );
216 }
217
218 /**
219 * The function checks if a given value is an array.
220 *
221 * Usage: is_array:{value}
222 *
223 * @param mixed $value is the variable that will be checked if it is an array or not.
224 *
225 * @return bool A boolean value is being returned, indicating whether the input value is a valid
226 * boolean or not.
227 */
228 public static function is_array( $value ): bool {
229 return is_array( $value );
230 }
231
232 /**
233 * The function checks if a given date string is valid according to a specified format in PHP.
234 *
235 * Rules: date_format:Y-m-d
236 *
237 * @since 2.6.0
238 *
239 * @param string $date_string is a string representing a date in a specific format. For example,
240 * "2022-01-31" or "31/01/2022".
241 * @param string $format The format parameter is a string that specifies the expected format of the date
242 * string. It uses the same format as the PHP date() function, with placeholders for different parts of
243 * the date (e.g. "Y" for the year, "m" for the month, "d" for the day.
244 *
245 * @return bool A boolean value (true or false) is being returned, depending on whether the given date
246 * string is valid according to the specified format.
247 */
248 public static function is_valid_date( $date_string, $format ): bool {
249 $date_string = gmdate( $format, strtotime( $date_string ) );
250 $date_object = \DateTime::createFromFormat( $format, $date_string );
251 $formatted_date = is_object( $date_object ) ? $date_object->format( $format ) : null;
252
253 return $date_object && $formatted_date === $date_string ? true : false;
254 }
255
256 /**
257 * Check if user exists
258 *
259 * Rules: user_exists:{user_id}
260 *
261 * @param integer $user_id user id.
262 * @return boolean
263 */
264 public static function is_user_exists( int $user_id ): bool {
265 $user = get_user_by( 'id', $user_id );
266 return $user ? true : false;
267 }
268
269 /**
270 * Check a table has record.
271 *
272 * @since 2.7.0
273 *
274 * @param string $table table name with prefix or without.
275 * @param string $column table column name.
276 * @param mixed $value table column value.
277 *
278 * @return boolean
279 */
280 public static function has_record( $table, $column, $value ) {
281 global $wpdb;
282 $table_prefix = $wpdb->prefix;
283 if ( strpos( $table, $table_prefix ) !== 0 ) {
284 $table = $table_prefix . $table;
285 }
286
287 $record = QueryHelper::get_row( $table, array( $column => $value ), $column );
288 return $record ? true : false;
289 }
290 }
291