ValidationHelper.php
246 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 | foreach ( $nested_rules as $nested_rule ) { |
| 48 | switch ( $nested_rule ) { |
| 49 | case 'required': |
| 50 | if ( ! self::has_key( $key, $data ) || self::is_empty( $data[ $key ] ) ) { |
| 51 | $validation_pass = false; |
| 52 | $validation_errors[ $key ] = $key . __( ' is required', 'tutor' ); |
| 53 | } |
| 54 | break; |
| 55 | case 'numeric': |
| 56 | if ( ! self::is_numeric( $data[ $key ] ) ) { |
| 57 | $validation_pass = false; |
| 58 | $validation_errors[ $key ] = $key . __( ' is not numeric', 'tutor' ); |
| 59 | } |
| 60 | break; |
| 61 | case 'min_length': |
| 62 | if ( strlen( $data[ $key ] ) < $nested_rules[1] ) { |
| 63 | $validation_pass = false; |
| 64 | $validation_errors[ $key ] = $key . __( ' minimum length is ', 'tutor' ) . $nested_rules[1]; |
| 65 | } |
| 66 | break; |
| 67 | case 'mimes': |
| 68 | $extensions = explode( ',', $nested_rules[1] ); |
| 69 | if ( ! self::in_array( $data[ $key ], $extensions ) ) { |
| 70 | $validation_pass = false; |
| 71 | $validation_errors[ $key ] = $key . __( ' extension is not valid', 'tutor' ); |
| 72 | } |
| 73 | break; |
| 74 | case 'match_string': |
| 75 | $strings = explode( ',', $nested_rules[1] ); |
| 76 | if ( ! self::in_array( $data[ $key ], $strings ) ) { |
| 77 | $validation_pass = false; |
| 78 | $validation_errors[ $key ] = $key . __( ' string is not valid', 'tutor' ); |
| 79 | } |
| 80 | break; |
| 81 | case 'boolean': |
| 82 | if ( ! self::is_boolean( $data[ $key ] ) ) { |
| 83 | $validation_pass = false; |
| 84 | $validation_errors[ $key ] = $key . __( ' is not boolean', 'tutor' ); |
| 85 | } |
| 86 | break; |
| 87 | case 'is_array': |
| 88 | if ( ! self::is_array( $data[ $key ] ) ) { |
| 89 | $validation_pass = false; |
| 90 | $validation_errors[ $key ] = $key . __( ' is not an array', 'tutor' ); |
| 91 | } |
| 92 | break; |
| 93 | case 'date_format': |
| 94 | $format = $nested_rules[1]; |
| 95 | if ( ! self::is_valid_date( $data[ $key ], $format ) ) { |
| 96 | $validation_pass = false; |
| 97 | $validation_errors[ $key ] = $key . __( ' invalid date format', 'tutor' ); |
| 98 | } |
| 99 | break; |
| 100 | case 'user_exists': |
| 101 | $user_id = (int) $data[ $key ]; |
| 102 | $is_exists = self::is_user_exists( $user_id ); |
| 103 | if ( ! $is_exists ) { |
| 104 | $validation_pass = false; |
| 105 | $validation_errors[ $key ] = $key . __( ' user does not exist', 'tutor' ); |
| 106 | } |
| 107 | break; |
| 108 | default: |
| 109 | // code... |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | $response = array( |
| 116 | 'success' => $validation_pass, |
| 117 | 'errors' => $validation_errors, |
| 118 | ); |
| 119 | return (object) $response; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Check if value is numeric |
| 124 | * |
| 125 | * Rules: numeric |
| 126 | * |
| 127 | * @param mixed $value value to check. |
| 128 | * |
| 129 | * @return boolean |
| 130 | */ |
| 131 | public static function is_numeric( $value ): bool { |
| 132 | return is_numeric( $value ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Check if value is empty |
| 137 | * |
| 138 | * Value will be considered empty if it is either null or empty string. |
| 139 | * |
| 140 | * Rules: required |
| 141 | * |
| 142 | * @param mixed $value value to check. |
| 143 | * |
| 144 | * @return boolean |
| 145 | */ |
| 146 | public static function is_empty( $value ): bool { |
| 147 | return '' === $value || is_null( $value ) ? true : false; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Check if array has key |
| 152 | * |
| 153 | * @param string $key key to check. |
| 154 | * @param array $array_assoc array where to check. |
| 155 | * |
| 156 | * @return boolean |
| 157 | */ |
| 158 | public static function has_key( string $key, array $array_assoc ): bool { |
| 159 | return isset( $array_assoc[ $key ] ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check if element has in array |
| 164 | * |
| 165 | * Rules: match_string:{value1},{value2}", |
| 166 | * |
| 167 | * @param string $key key to check. |
| 168 | * @param array $array array where to check. |
| 169 | * |
| 170 | * @return boolean |
| 171 | */ |
| 172 | public static function in_array( string $key, array $array ): bool { |
| 173 | return in_array( $key, $array ); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * The function checks if a given value is a boolean. |
| 179 | * |
| 180 | * Considered values: array( 1, 0, 'true', 'false', true, false ), any value |
| 181 | * except these will be not counted as boolean |
| 182 | * |
| 183 | * Rules: boolean |
| 184 | * |
| 185 | * @param mixed $value is the variable that will be checked if it is a boolean value or not. |
| 186 | * |
| 187 | * @return bool A boolean value is being returned, indicating whether the input value is a valid |
| 188 | * boolean or not. |
| 189 | */ |
| 190 | public static function is_boolean( $value ): bool { |
| 191 | $allowed_booleans = array( 1, 0, '1', '0', 'true', 'false', true, false ); |
| 192 | return in_array( $value, $allowed_booleans, true ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * The function checks if a given value is an array. |
| 197 | * |
| 198 | * Usage: is_array:{value} |
| 199 | * |
| 200 | * @param mixed $value is the variable that will be checked if it is an array or not. |
| 201 | * |
| 202 | * @return bool A boolean value is being returned, indicating whether the input value is a valid |
| 203 | * boolean or not. |
| 204 | */ |
| 205 | public static function is_array( $value ): bool { |
| 206 | return is_array( $value ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * The function checks if a given date string is valid according to a specified format in PHP. |
| 211 | * |
| 212 | * Rules: date_format:Y-m-d |
| 213 | * |
| 214 | * @since 2.6.0 |
| 215 | * |
| 216 | * @param string $date_string is a string representing a date in a specific format. For example, |
| 217 | * "2022-01-31" or "31/01/2022". |
| 218 | * @param string $format The format parameter is a string that specifies the expected format of the date |
| 219 | * string. It uses the same format as the PHP date() function, with placeholders for different parts of |
| 220 | * the date (e.g. "Y" for the year, "m" for the month, "d" for the day. |
| 221 | * |
| 222 | * @return bool A boolean value (true or false) is being returned, depending on whether the given date |
| 223 | * string is valid according to the specified format. |
| 224 | */ |
| 225 | public static function is_valid_date( $date_string, $format ): bool { |
| 226 | $date_string = gmdate( $format, strtotime( $date_string ) ); |
| 227 | $date_object = \DateTime::createFromFormat( $format, $date_string ); |
| 228 | $formatted_date = is_object( $date_object ) ? $date_object->format( $format ) : null; |
| 229 | |
| 230 | return $date_object && $formatted_date === $date_string ? true : false; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Check if user exists |
| 235 | * |
| 236 | * Rules: user_exists:{user_id} |
| 237 | * |
| 238 | * @param integer $user_id user id. |
| 239 | * @return boolean |
| 240 | */ |
| 241 | public static function is_user_exists( int $user_id ): bool { |
| 242 | $user = get_user_by( 'id', $user_id ); |
| 243 | return $user ? true : false; |
| 244 | } |
| 245 | } |
| 246 |