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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
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