PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.27
Booktics – Appointment Booking Calendar for Service Businesses v1.0.27
1.0.29 1.0.28 1.0.27 1.0.26 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 All 31 releases
← All changes | base/validation/validator.php +168 -168 1.0.1 → 1.0.27 View file →
@@ -1,168 +1,168 @@
1 -<?php
2 -/**
3 - * Input Validation class
4 - *
5 - * @package Booktics\Validation
6 - */
7 -namespace Booktics\Validation;
8 -
9 -use Booktics\Validation\Rules\Rule;
10 -use WP_Error;
11 -
12 -class Validator {
13 - /**
14 - * Store validation data
15 - *
16 - * @var array
17 - */
18 - protected array $data = array();
19 -
20 - /**
21 - * Store validation rules
22 - *
23 - * @var array
24 - */
25 - protected array $rules = array();
26 -
27 - /**
28 - * Store aliases
29 - *
30 - * @var array
31 - */
32 - protected array $aliases = array();
33 -
34 - /**
35 - * Store errors
36 - *
37 - * @var Object
38 - */
39 - protected $errors;
40 -
41 - /**
42 - * Initialize
43 - *
44 - * @param array $data
45 - *
46 - * @return void
47 - */
48 - public function __construct( array $data ) {
49 - $this->data = $data;
50 - $this->errors = new WP_Error();
51 - }
52 -
53 - /**
54 - * Set validation rules
55 - *
56 - * @param array $rules
57 - *
58 - * @return void
59 - */
60 - public function set_rules( array $rules ) {
61 - $this->rules = $rules;
62 - }
63 -
64 - /**
65 - * Set field validation aliases
66 - *
67 - * @param array $aliases
68 - *
69 - * @return void
70 - */
71 - public function set_aliases( array $aliases ) {
72 - $this->aliases = $aliases;
73 - }
74 -
75 - /**
76 - * Validate data
77 - *
78 - * @return bool
79 - */
80 - public function validate() {
81 - foreach ( $this->rules as $field => $rules ) {
82 - foreach ( $this->resolve_rules( $rules ) as $rule ) {
83 - $this->validate_rule( $field, $rule );
84 - }
85 - }
86 -
87 - return ! $this->errors->has_errors();
88 - }
89 -
90 - /**
91 - * Get validation error
92 - *
93 - * @return WP_Error
94 - */
95 - public function get_error() {
96 - return $this->errors;
97 - }
98 -
99 - /**
100 - * Validate a single rule
101 - *
102 - * @param string $field
103 - * @param Rule $rule
104 - *
105 - * @return bool
106 - */
107 - protected function validate_rule( string $field, Rule $rule ) {
108 -
109 - if ( ! $rule->passes( $field, $this->get_field_value( $field ) ) ) {
110 - $this->errors->add( $field, $rule->message( $this->get_field_alias( $field ) ), array( 'status' => '400' ) );
111 - }
112 - return true;
113 - }
114 -
115 - /**
116 - * Resolve rules
117 - *
118 - * @param array $rules
119 - *
120 - * @return array
121 - */
122 - protected function resolve_rules( array $rules ) {
123 - return array_map(
124 - function ( $rule ) {
125 - if ( is_string( $rule ) ) {
126 - return $this->get_rule_from_string( $rule );
127 - }
128 - return $rule;
129 - }, $rules
130 - );
131 - }
132 -
133 - /**
134 - * Extract rules from string value
135 - *
136 - * @param array | string $rule
137 - *
138 - * @return Rule
139 - */
140 - protected function get_rule_from_string( $rule ) {
141 - $exploded = explode( ':', $rule );
142 - $rule = $exploded[0];
143 - $options = explode( ',', end( $exploded ) );
144 - return Rule_Map::resolve_rule_map( $rule, $options );
145 - }
146 -
147 - /**
148 - * Get field value
149 - *
150 - * @param string $field
151 - *
152 - * @return mixed
153 - */
154 - protected function get_field_value( string $field ) {
155 - return $this->data[ $field ] ?? null;
156 - }
157 -
158 - /**
159 - * Get field alias
160 - *
161 - * @param string $field
162 - *
163 - * @return mixed
164 - */
165 - protected function get_field_alias( string $field ) {
166 - return $this->aliases[ $field ] ?? $field;
167 - }
168 -}
1 +<?php
2 +/**
3 + * Input Validation class
4 + *
5 + * @package Booktics\Validation
6 + */
7 +namespace Booktics\Validation;
8 +
9 +use Booktics\Validation\Rules\Rule;
10 +use WP_Error;
11 +
12 +class Validator {
13 + /**
14 + * Store validation data
15 + *
16 + * @var array
17 + */
18 + protected array $data = array();
19 +
20 + /**
21 + * Store validation rules
22 + *
23 + * @var array
24 + */
25 + protected array $rules = array();
26 +
27 + /**
28 + * Store aliases
29 + *
30 + * @var array
31 + */
32 + protected array $aliases = array();
33 +
34 + /**
35 + * Store errors
36 + *
37 + * @var Object
38 + */
39 + protected $errors;
40 +
41 + /**
42 + * Initialize
43 + *
44 + * @param array $data
45 + *
46 + * @return void
47 + */
48 + public function __construct( array $data ) {
49 + $this->data = $data;
50 + $this->errors = new WP_Error();
51 + }
52 +
53 + /**
54 + * Set validation rules
55 + *
56 + * @param array $rules
57 + *
58 + * @return void
59 + */
60 + public function set_rules( array $rules ) {
61 + $this->rules = $rules;
62 + }
63 +
64 + /**
65 + * Set field validation aliases
66 + *
67 + * @param array $aliases
68 + *
69 + * @return void
70 + */
71 + public function set_aliases( array $aliases ) {
72 + $this->aliases = $aliases;
73 + }
74 +
75 + /**
76 + * Validate data
77 + *
78 + * @return bool
79 + */
80 + public function validate() {
81 + foreach ( $this->rules as $field => $rules ) {
82 + foreach ( $this->resolve_rules( $rules ) as $rule ) {
83 + $this->validate_rule( $field, $rule );
84 + }
85 + }
86 +
87 + return ! $this->errors->has_errors();
88 + }
89 +
90 + /**
91 + * Get validation error
92 + *
93 + * @return WP_Error
94 + */
95 + public function get_error() {
96 + return $this->errors;
97 + }
98 +
99 + /**
100 + * Validate a single rule
101 + *
102 + * @param string $field
103 + * @param Rule $rule
104 + *
105 + * @return bool
106 + */
107 + protected function validate_rule( string $field, Rule $rule ) {
108 +
109 + if ( ! $rule->passes( $field, $this->get_field_value( $field ) ) ) {
110 + $this->errors->add( $field, $rule->message( $this->get_field_alias( $field ) ), array( 'status' => '400' ) );
111 + }
112 + return true;
113 + }
114 +
115 + /**
116 + * Resolve rules
117 + *
118 + * @param array $rules
119 + *
120 + * @return array
121 + */
122 + protected function resolve_rules( array $rules ) {
123 + return array_map(
124 + function ( $rule ) {
125 + if ( is_string( $rule ) ) {
126 + return $this->get_rule_from_string( $rule );
127 + }
128 + return $rule;
129 + }, $rules
130 + );
131 + }
132 +
133 + /**
134 + * Extract rules from string value
135 + *
136 + * @param array | string $rule
137 + *
138 + * @return Rule
139 + */
140 + protected function get_rule_from_string( $rule ) {
141 + $exploded = explode( ':', $rule );
142 + $rule = $exploded[0];
143 + $options = explode( ',', end( $exploded ) );
144 + return Rule_Map::resolve_rule_map( $rule, $options );
145 + }
146 +
147 + /**
148 + * Get field value
149 + *
150 + * @param string $field
151 + *
152 + * @return mixed
153 + */
154 + protected function get_field_value( string $field ) {
155 + return $this->data[ $field ] ?? null;
156 + }
157 +
158 + /**
159 + * Get field alias
160 + *
161 + * @param string $field
162 + *
163 + * @return mixed
164 + */
165 + protected function get_field_alias( string $field ) {
166 + return $this->aliases[ $field ] ?? $field;
167 + }
168 +}