PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.62
Timetics – Appointment Booking Calendar & Scheduling v1.0.62
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / utils / validator.php

validator.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.62, at utils/validator.php

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