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 |