Completed
Push — master ( 6773cf...cb49b4 )
by Maciej
15s
created

FormComposite::is_valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 3.

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.

Loading history...
2
namespace WFV;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Artisan\FormArtisan;
6
use WFV\Contract\ValidateInterface;
7
use WFV\RuleFactory;
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.2
38
	 * @access protected
39
	 * @var WFV\RuleFactory
40
	 */
41
	protected $factory;
42
43
	/**
44
	 *
45
	 *
46
	 * @since 0.11.0
47
	 * @access protected
48
	 * @var Validator
49
	 */
50
	protected $validator;
51
52
	/**
53
	 *
54
	 *
55
	 * @since 0.10.0
56
	 *
57
	 * @param ArtisanInterface $builder
58
	 * @param string $action
59
	 */
60
	public function __construct( FormArtisan $builder, $action ) {
61
		$this->alias = $action;
62
		$this->collection = $builder->collection;
63
		$this->factory = $builder->factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $builder->factory of type object<WFV\RuleFactory> is incompatible with the declared type object<WFV\WFV\RuleFactory> of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
		$this->validator = $builder->validator;
65
	}
66
67
	/**
68
	 * Convenience method to repopulate checkbox input
69
	 *
70
	 * @since 0.10.0
71
	 *
72
	 * @param string $field Field name.
73
	 * @param string $value Value to compare against.
74
	 * @return string|null
75
	 */
76
	public function checked_if( $field = null, $value = null ) {
77
		return $this->string_or_null( 'checked', $field, $value );
78
	}
79
80
	/**
81
	 * Echo the encoded value of given field from a callback
82
	 * Default callback is esc_html()
83
	 * Also returns the encoded string for assignment
84
	 *
85
	 * @since 0.10.1
86
	 *
87
	 * @param string (optional) $field
88
	 * @param callable (optional) $callback
89
	 * @return string
90
	 */
91
	public function display( $field = null, callable $callback = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
		echo $input = $this->utilize('input')->escape( $field );
93
		return $input;
94
	}
95
96
	/**
97
	 * Use error collection
98
	 *
99
	 *
100
	 * @since 0.10.0
101
	 *
102
	 * @return WFV\Collection\ErrorCollection
103
	 */
104
	public function errors() {
105
		return $this->utilize('errors');
106
	}
107
108
	/**
109
	 * Use input collection
110
	 *
111
	 * @since 0.10.0
112
	 *
113
	 * @return WFV\Collection\InputCollection
114
	 */
115
	public function input() {
116
		return $this->utilize('input');
117
	}
118
119
	/**
120
	 * Use rules collection
121
	 *
122
	 * @since 0.11.0
123
	 *
124
	 * @return WFV\Collection\RuleCollection
125
	 */
126
	public function rules() {
127
		return $this->utilize('rules');
128
	}
129
130
	/**
131
	 * Convenience method to repopulate select input
132
	 *
133
	 * @since 0.10.0
134
	 *
135
	 * @param string $field Field name.
136
	 * @param string $value Value to compare against.
137
	 * @return string|null
138
	 */
139
	public function selected_if( $field = null, $value = null ) {
140
		return $this->string_or_null( 'selected', $field, $value );
141
	}
142
143
	/**
144
	 * Convienience method to print the hidden fields
145
	 *  for token and action
146
	 *
147
	 * @since 0.10.0
148
	 *
149
	 */
150
	public function token_fields() {
151
		// TODO - Move markup into something - perhaps a renderable interface?
152
		$token_name = $this->alias . '_token';
153
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
154
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
155
	}
156
157
	/**
158
	 * Perform the validation cycle
159
	 *
160
	 * @since 0.11.0
161
	 *
162
	 * @return bool
163
	 */
164
	public function validate() {
165
		$rule_collection = $this->utilize('rules');
166
		$rules = $rule_collection->get_array( true );
167
168
		foreach( $rules as $field => $ruleset ) {
169
			$input = $this->field_value( $field );
170
			$optional = $rule_collection->is_optional( $field );
171
172
			foreach( $ruleset as $index => $rule ) {
173
				$params = $rule_collection->get_params( $field, $index );
174
				$this->validator->validate( $this->factory->get( $rule ), $field, $input, $optional, $params );
0 ignored issues
show
Bug introduced by
It seems like $input defined by $this->field_value($field) on line 169 can also be of type null; however, WFV\Validator::validate() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
175
			}
176
		}
177
		return $this->is_valid();
178
	}
179
180
	/**
181
	 * Returns the input value for a field
182
	 * When not present, returns null
183
	 *
184
	 * @since 0.11.0
185
	 * @access protected
186
	 *
187
	 * @param string $field
188
	 * @return string|array|null
189
	 */
190
	protected function field_value( $field ) {
191
		$input = $this->utilize('input');
192
		if( $input->has( $field ) ) {
193
			$input = $input->get_array();
194
			return $input[ $field ];
195
		}
196
		return null;
197
	}
198
199
	/**
200
	 * Check if the validation passed or failed
201
	 * Sets the error msgs if a fail
202
	 * Trigger pass or fail action
203
	 *
204
	 * @since 0.11.0
205
	 *
206
	 * @return bool
207
	 */
208
	protected function is_valid() {
209
		$is_valid = $this->validator->is_valid();
210
		if( false === $is_valid ) {
211
			$this->utilize('errors')->set_errors( $this->validator->errors() );
212
		}
213
		$this->trigger_post_validate_action( $is_valid );
214
		return $is_valid;
215
	}
216
217
	/**
218
	 *
219
	 *
220
	 * @since 0.10.0
221
	 * @access protected
222
	 *
223
	 * @param string $response
224
	 * @param string (optional) $field
225
	 * @param string (optional) $value
226
	 * @return string|null
227
	 */
228
	protected function string_or_null( $response, $field = null, $value = null ) {
229
		$input = $this->utilize('input');
230
		return ( $input->contains( $field, $value ) ) ? $response : null;
231
	}
232
233
	/**
234
	 * Trigger action hook for validation pass or fail
235
	 *
236
	 * @since 0.10.0
237
	 * @access protected
238
	 *
239
	 * @param bool $is_valid
240
	 */
241
	protected function trigger_post_validate_action( $is_valid = false ) {
242
		$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail';
243
		do_action( $action, $this );
244
	}
245
246
	/**
247
	 * Use a component.
248
	 *
249
	 * @since 0.10.0
250
	 * @access protected
251
	 *
252
	 * @param string $component Key indentifier.
253
	 */
254
	protected function utilize( $component ) {
255
		return $this->collection[ $component ];
256
	}
257
}
258