| 1 |
<?php |
| 2 |
/** |
| 3 |
* Validator Traits |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Utils; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
|
| 11 |
/** |
| 12 |
* Validator trait |
| 13 |
*/ |
| 14 |
trait Validator { |
| 15 |
/** |
| 16 |
* Store WP_Error Object |
| 17 |
* |
| 18 |
* @var WP_Error |
| 19 |
*/ |
| 20 |
private $error; |
| 21 |
|
| 22 |
/** |
| 23 |
* Validation rules |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $rules = ['required', 'min', 'max', 'email', 'timezone']; |
| 28 |
|
| 29 |
/** |
| 30 |
* Validation error messages |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $messages = [ |
| 35 |
'required' => ':field is required', |
| 36 |
'min' => ':field is minimum of :satisfier', |
| 37 |
'max' => ':field is maximum of :satisfier', |
| 38 |
'email' => 'This is email is invalid', |
| 39 |
'timezone' => 'This is not valid timezone', |
| 40 |
]; |
| 41 |
|
| 42 |
/** |
| 43 |
* Validate fields |
| 44 |
* |
| 45 |
* @param array $rules The rules that will be compared with field value |
| 46 |
* @param array $fields Input fields |
| 47 |
* |
| 48 |
* @return bool | WP_Error |
| 49 |
*/ |
| 50 |
public function validate( $rules, $fields ) { |
| 51 |
$this->error = new WP_Error(); |
| 52 |
|
| 53 |
foreach ( $fields as $field => $value ) { |
| 54 |
if ( array_key_exists( $field, $rules ) ) { |
| 55 |
$this->check( [ |
| 56 |
'field' => $field, |
| 57 |
'value' => $value, |
| 58 |
'rules' => $rules[$field], |
| 59 |
] ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( $this->error->has_errors() ) { |
| 64 |
return $this->error; |
| 65 |
} |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Check the rules with field value |
| 72 |
* |
| 73 |
* @param array $item |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
private function check( $item ) { |
| 78 |
$field = $item['field']; |
| 79 |
$value = $item['value']; |
| 80 |
$rules = $item['rules']; |
| 81 |
|
| 82 |
foreach ( $rules as $rule => $satisfier ) { |
| 83 |
if ( ! in_array( $rule, $this->rules ) ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! call_user_func_array( [$this, $rule], [$field, $value, $satisfier] ) ) { |
| 88 |
$code = $item['field'] . '_' . $rule; |
| 89 |
$message = str_replace( [':field', ':satisfier'], [$field, $satisfier], $this->messages[$rule] ); |
| 90 |
|
| 91 |
$this->error->add( $code, $message ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Required validation |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
private function required( $field, $value, $satisfier ) { |
| 102 |
return ! empty( $value ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Minimum length validation |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
private function min( $field, $value, $satisfier ) { |
| 111 |
return mb_strlen( $value ) >= $satisfier; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Maximum length validation |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
private function max( $field, $value, $satisfier ) { |
| 120 |
return mb_strlen( $value ) <= $satisfier; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Email validation |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
private function email( $field, $value, $satisfier ) { |
| 129 |
return filter_var( $value, FILTER_VALIDATE_EMAIL ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Validate timezone |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
private function timezone( $field, $value, $satisfier ) { |
| 138 |
try { |
| 139 |
new \DateTimeZone( $value ); |
| 140 |
} catch ( \Exception $e ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
return true; |
| 145 |
} |
| 146 |
} |
| 147 |
|