PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.39
Timetics – Appointment Booking Calendar & Scheduling v1.0.39
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.39, at utils/validator.php

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