PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.25
Booktics – Appointment Booking Calendar for Service Businesses v1.0.25
1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 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.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / utils / validator.php

validator.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.25, 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 Booktics
6 */
7 namespace Booktics\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 = array( 'required', 'min', 'max', 'email', 'timezone' );
28
29 /**
30 * Validation error messages
31 *
32 * @var array
33 */
34 private $messages = array(
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 array(
57 'field' => $field,
58 'value' => $value,
59 'rules' => $rules[ $field ],
60 )
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, true ) ) {
86 continue;
87 }
88
89 if ( ! call_user_func_array( array( $this, $rule ), array( $field, $value, $satisfier ) ) ) {
90 $code = $item['field'] . '_' . $rule;
91 $message = str_replace( array( ':field', ':satisfier' ), array( $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