|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\Artisan\FormArtisan; |
|
6
|
|
|
use WFV\Contract\ValidateInterface; |
|
7
|
|
|
use WFV\Factory\ValidatorFactory; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Form Composition |
|
11
|
|
|
* |
|
12
|
|
|
* @since 0.10.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class FormComposite { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.10.0 |
|
20
|
|
|
* @access private |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $alias; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* |
|
28
|
|
|
* @since 0.10.0 |
|
29
|
|
|
* @access protected |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $collection; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* |
|
37
|
|
|
* @since 0.11.0 |
|
38
|
|
|
* @access protected |
|
39
|
|
|
* @var Validator |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $validator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* |
|
46
|
|
|
* @since 0.10.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @param ArtisanInterface $builder |
|
49
|
|
|
* @param string $action |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( FormArtisan $builder, $action ) { |
|
52
|
|
|
$this->alias = $action; |
|
53
|
|
|
$this->collection = $builder->collection; |
|
54
|
|
|
$this->validator = $builder->validator; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Convenience method to repopulate checkbox input |
|
59
|
|
|
* |
|
60
|
|
|
* @since 0.10.0 |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $field Field name. |
|
63
|
|
|
* @param string $value Value to compare against. |
|
64
|
|
|
* @return string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function checked_if( $field = null, $value = null ) { |
|
67
|
|
|
return $this->string_or_null( 'checked', $field, $value ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Echo the encoded value of given field from a callback |
|
72
|
|
|
* Default callback is esc_html() |
|
73
|
|
|
* Also returns the encoded string for assignment |
|
74
|
|
|
* |
|
75
|
|
|
* @since 0.10.1 |
|
76
|
|
|
* |
|
77
|
|
|
* @param string (optional) $field |
|
78
|
|
|
* @param callable (optional) $callback |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function display( $field = null, callable $callback = null ) { |
|
|
|
|
|
|
82
|
|
|
echo $input = $this->utilize('input')->escape( $field ); |
|
83
|
|
|
return $input; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Use error collection |
|
88
|
|
|
* |
|
89
|
|
|
* |
|
90
|
|
|
* @since 0.10.0 |
|
91
|
|
|
* |
|
92
|
|
|
* @return WFV\Collection\ErrorCollection |
|
93
|
|
|
*/ |
|
94
|
|
|
public function errors() { |
|
95
|
|
|
return $this->utilize('errors'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Use input collection |
|
100
|
|
|
* |
|
101
|
|
|
* @since 0.10.0 |
|
102
|
|
|
* |
|
103
|
|
|
* @return WFV\Collection\InputCollection |
|
104
|
|
|
*/ |
|
105
|
|
|
public function input() { |
|
106
|
|
|
return $this->utilize('input'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Check if the validation passed or failed |
|
111
|
|
|
* Sets the error msgs if a fail |
|
112
|
|
|
* Trigger pass or fail action |
|
113
|
|
|
* |
|
114
|
|
|
* @since 0.11.0 |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function is_valid() { |
|
119
|
|
|
$is_valid = $this->validator->is_valid(); |
|
120
|
|
|
if( false === $is_valid ) { |
|
121
|
|
|
$this->utilize('errors')->set_errors( $this->validator->errors() ); |
|
122
|
|
|
} |
|
123
|
|
|
$this->trigger_post_validate_action( $is_valid ); |
|
124
|
|
|
return $is_valid; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Use rules collection |
|
129
|
|
|
* |
|
130
|
|
|
* @since 0.11.0 |
|
131
|
|
|
* |
|
132
|
|
|
* @return WFV\Collection\RuleCollection |
|
133
|
|
|
*/ |
|
134
|
|
|
public function rules() { |
|
135
|
|
|
return $this->utilize('rules'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Convenience method to repopulate select input |
|
140
|
|
|
* |
|
141
|
|
|
* @since 0.10.0 |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $field Field name. |
|
144
|
|
|
* @param string $value Value to compare against. |
|
145
|
|
|
* @return string|null |
|
146
|
|
|
*/ |
|
147
|
|
|
public function selected_if( $field = null, $value = null ) { |
|
148
|
|
|
return $this->string_or_null( 'selected', $field, $value ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Convienience method to print the hidden fields |
|
153
|
|
|
* for token and action |
|
154
|
|
|
* |
|
155
|
|
|
* @since 0.10.0 |
|
156
|
|
|
* |
|
157
|
|
|
*/ |
|
158
|
|
|
public function token_fields() { |
|
159
|
|
|
// TODO - Move markup into something - perhaps a renderable interface? |
|
160
|
|
|
$token_name = $this->alias . '_token'; |
|
161
|
|
|
echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false ); |
|
162
|
|
|
echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Perform the validation cycle |
|
167
|
|
|
* |
|
168
|
|
|
* @since 0.11.0 |
|
169
|
|
|
* |
|
170
|
|
|
* @param ValidatorFactory $factory |
|
171
|
|
|
* @return self |
|
172
|
|
|
*/ |
|
173
|
|
|
public function validate( ValidatorFactory $factory ) { |
|
174
|
|
|
$rule_collection = $this->utilize('rules'); |
|
175
|
|
|
$rules = $rule_collection->get_array( true ); |
|
176
|
|
|
|
|
177
|
|
|
foreach( $rules as $field => $ruleset ) { |
|
178
|
|
|
$input = $this->field_value( $field ); |
|
179
|
|
|
$optional = $rule_collection->is_optional( $field ); |
|
180
|
|
|
|
|
181
|
|
|
foreach( $ruleset as $index => $rule ) { |
|
182
|
|
|
$params = $rule_collection->get_params( $field, $index ); |
|
183
|
|
|
$this->validator->validate( $factory->get( $rule ), $field, $input, $optional, $params ); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the input value for a field |
|
191
|
|
|
* When not present, returns null |
|
192
|
|
|
* |
|
193
|
|
|
* @since 0.11.0 |
|
194
|
|
|
* @access protected |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $field |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function field_value( $field ) { |
|
199
|
|
|
$input = $this->utilize('input'); |
|
200
|
|
|
if( $input->has( $field ) ) { |
|
201
|
|
|
$input = $input->get_array( false ); |
|
202
|
|
|
return $input[ $field ]; |
|
203
|
|
|
} |
|
204
|
|
|
return null; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* |
|
209
|
|
|
* |
|
210
|
|
|
* @since 0.10.0 |
|
211
|
|
|
* @access protected |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $response |
|
214
|
|
|
* @param string (optional) $field |
|
215
|
|
|
* @param string (optional) $value |
|
216
|
|
|
* @return string|null |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function string_or_null( $response, $field = null, $value = null ) { |
|
219
|
|
|
$input = $this->utilize('input'); |
|
220
|
|
|
return ( $input->contains( $field, $value ) ) ? $response : null; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Trigger action hook for validation pass or fail |
|
225
|
|
|
* |
|
226
|
|
|
* @since 0.10.0 |
|
227
|
|
|
* @access protected |
|
228
|
|
|
* |
|
229
|
|
|
* @param bool $is_valid |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function trigger_post_validate_action( $is_valid = false ) { |
|
232
|
|
|
$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail'; |
|
233
|
|
|
do_action( $action, $this ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Use a component. |
|
238
|
|
|
* |
|
239
|
|
|
* @since 0.10.0 |
|
240
|
|
|
* @access protected |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $component Key indentifier. |
|
243
|
|
|
*/ |
|
244
|
|
|
protected function utilize( $component ) { |
|
245
|
|
|
return $this->collection[ $component ]; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.