|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\Artisan\FormArtisan; |
|
6
|
|
|
use WFV\Contract\FormInterface; |
|
7
|
|
|
use WFV\Contract\ValidateInterface; |
|
8
|
|
|
use WFV\Factory\ValidatorFactory; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Form Composition |
|
12
|
|
|
* |
|
13
|
|
|
* @since 0.10.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class FormComposite { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* |
|
20
|
|
|
* @since 0.10.0 |
|
21
|
|
|
* @access protected |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $alias; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* |
|
29
|
|
|
* @since 0.10.0 |
|
30
|
|
|
* @access protected |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $collection; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* |
|
38
|
|
|
* @since 0.11.0 |
|
39
|
|
|
* @access protected |
|
40
|
|
|
* @var Validator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $validator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* |
|
47
|
|
|
* @since 0.10.0 |
|
48
|
|
|
* |
|
49
|
|
|
* @param ArtisanInterface $builder |
|
50
|
|
|
* @param string $action |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( FormArtisan $builder, $action ) { |
|
53
|
|
|
$this->alias = $action; |
|
54
|
|
|
$this->collection = $builder->collection; |
|
55
|
|
|
$this->validator = $builder->validator; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the validation passed or failed |
|
60
|
|
|
* Sets the error msgs if a fail |
|
61
|
|
|
* Trigger pass or fail action |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.11.0 |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function valid() { |
|
68
|
|
|
$is_valid = $this->validator->is_valid(); |
|
69
|
|
|
if( false === $is_valid ) { |
|
70
|
|
|
$this->utilize('errors')->set_errors( $this->validator->errors() ); |
|
71
|
|
|
} |
|
72
|
|
|
$this->trigger_post_validate_action( $is_valid ); |
|
73
|
|
|
return $is_valid; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Perform the validation cycle |
|
78
|
|
|
* |
|
79
|
|
|
* @since 0.11.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @param ValidatorFactory $factory |
|
82
|
|
|
* @return self |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function validate( ValidatorFactory $factory ) { |
|
85
|
|
|
$rule_collection = $this->utilize('rules'); |
|
86
|
|
|
$rules = $rule_collection->get_array( true ); |
|
87
|
|
|
|
|
88
|
|
|
foreach( $rules as $field => $ruleset ) { |
|
89
|
|
|
$input = $this->field_value( $field ); |
|
90
|
|
|
$optional = $rule_collection->is_optional( $field ); |
|
91
|
|
|
|
|
92
|
|
|
foreach( $ruleset as $index => $rule ) { |
|
93
|
|
|
$params = $rule_collection->get_params( $field, $index ); |
|
94
|
|
|
$this->validator->validate( $factory->get( $rule ), $field, $input, $optional, $params ); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $this->valid(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the input value for a field |
|
102
|
|
|
* When not present, returns null |
|
103
|
|
|
* |
|
104
|
|
|
* @since 0.11.0 |
|
105
|
|
|
* @access protected |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $field |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function field_value( $field ) { |
|
110
|
|
|
$input = $this->utilize('input'); |
|
111
|
|
|
if( $input->has( $field ) ) { |
|
112
|
|
|
$input = $input->get_array( false ); |
|
113
|
|
|
return $input[ $field ]; |
|
114
|
|
|
} |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* |
|
120
|
|
|
* |
|
121
|
|
|
* @since 0.10.0 |
|
122
|
|
|
* @access protected |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $response |
|
125
|
|
|
* @param string (optional) $field |
|
126
|
|
|
* @param string (optional) $value |
|
127
|
|
|
* @return string|null |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function string_or_null( $response, $field = null, $value = null ) { |
|
130
|
|
|
return ( $this->utilize('input')->contains( $field, $value ) ) ? $response : null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Trigger action hook for validation pass or fail |
|
135
|
|
|
* |
|
136
|
|
|
* @since 0.10.0 |
|
137
|
|
|
* @access protected |
|
138
|
|
|
* |
|
139
|
|
|
* @param bool $is_valid |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function trigger_post_validate_action( $is_valid = false ) { |
|
142
|
|
|
$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail'; |
|
143
|
|
|
do_action( $action, $this ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Use a component. |
|
148
|
|
|
* |
|
149
|
|
|
* @since 0.10.0 |
|
150
|
|
|
* @access protected |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $component Key indentifier. |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function utilize( $component ) { |
|
155
|
|
|
return $this->collection[ $component ]; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
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.