PluginProbe
Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating / trunk
Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8
authorsy / utils / validator.php

validator.php in Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating trunk, at utils/validator.php

133 lines 2.9 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 Authorsy
6 */
7 namespace Authorsy\Utils;
8 defined( 'ABSPATH' ) || exit;
9
10 use WP_Error;
11
12 /**
13 * Validator trait
14 */
15 trait Validator {
16 /**
17 * Store WP_Error Object
18 *
19 * @var WP_Error
20 */
21 private $error;
22
23 /**
24 * Validation rules
25 *
26 * @var array
27 */
28 private $rules = ['required', 'min', 'max', 'email'];
29
30 /**
31 * Validation error messages
32 *
33 * @var array
34 */
35 private $messages = [
36 'required' => ':field is required',
37 'min' => ':field is minimum of :satisfier',
38 'max' => ':field is maximum of :satisfier',
39 'email' => 'This is email is invalid',
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