Completed
Push — master ( 362609...afa8c3 )
by Maciej
15s
created

FormComposite::input()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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 15 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\Abstraction\Composable;
6
use WFV\Artisan\FormArtisan;
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 extends Composable {
16
17
	/**
18
	 *
19
	 *
20
	 * @since 0.11.0
21
	 * @access protected
22
	 * @var Validator
23
	 */
24
	protected $validator;
25
26
	/**
27
	 *
28
	 *
29
	 * @since 0.10.0
30
	 *
31
	 * @param ArtisanInterface $builder
32
	 * @param string $action
33
	 */
34
	public function __construct( FormArtisan $builder, $action ) {
35
		$this->alias = $action;
36
		$this->install( $builder->collection );
37
		$this->validator = $builder->validator;
38
	}
39
40
	/**
41
	 * Convenience method to repopulate checkbox input
42
	 *
43
	 * @since 0.10.0
44
	 *
45
	 * @param string $field Field name.
46
	 * @param string $value Value to compare against.
47
	 * @return string|null
48
	 */
49
	public function checked_if( $field = null, $value = null ) {
50
		return $this->string_or_null( 'checked', $field, $value );
51
	}
52
53
	/**
54
	 * Echo the encoded value of given field from a callback
55
	 * Default callback is esc_html()
56
	 * Also returns the encoded string for assignment
57
	 *
58
	 * @since 0.10.1
59
	 *
60
	 * @param string (optional) $field
61
	 * @param callable (optional) $callback
62
	 * @return string
63
	 */
64
	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...
65
		echo $input = $this->utilize('input')->escape( $field );
66
		return $input;
67
	}
68
69
	/**
70
	 * Use error collection
71
	 *
72
	 *
73
	 * @since 0.10.0
74
	 *
75
	 * @return WFV\Collection\ErrorCollection
76
	 */
77
	public function errors() {
78
		return $this->utilize('errors');
79
	}
80
81
	/**
82
	 * Use input collection
83
	 *
84
	 * @since 0.10.0
85
	 *
86
	 * @return WFV\Collection\InputCollection
87
	 */
88
	public function input() {
89
		return $this->utilize('input');
90
	}
91
92
	/**
93
	 * Check if the validation passed or failed
94
	 * Sets the error msgs if a fail
95
	 * Trigger pass or fail action
96
	 *
97
	 * @since 0.11.0
98
	 *
99
	 * @return bool
100
	 */
101
	public function is_valid() {
102
		$is_valid = $this->validator->is_valid();
103
		if( false === $is_valid ) {
104
			$this->utilize('errors')->set_errors( $this->validator->errors() );
105
		}
106
		$this->trigger_post_validate_action( $is_valid );
107
		return $is_valid;
108
	}
109
110
	/**
111
	 * Use rules collection
112
	 *
113
	 * @since 0.11.0
114
	 *
115
	 * @return WFV\Collection\RuleCollection
116
	 */
117
	public function rules() {
118
		return $this->utilize('rules');
119
	}
120
121
	/**
122
	 * Convenience method to repopulate select input
123
	 *
124
	 * @since 0.10.0
125
	 *
126
	 * @param string $field Field name.
127
	 * @param string $value Value to compare against.
128
	 * @return string|null
129
	 */
130
	public function selected_if( $field = null, $value = null ) {
131
		return $this->string_or_null( 'selected', $field, $value );
132
	}
133
134
	/**
135
	 * Convienience method to print the hidden fields
136
	 *  for token and action
137
	 *
138
	 * @since 0.10.0
139
	 *
140
	 */
141
	public function token_fields() {
142
		// TODO - Move markup into something - perhaps a renderable interface?
143
		$token_name = $this->alias . '_token';
144
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
145
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
146
	}
147
148
	/**
149
	 * Perform the validation cycle
150
	 *
151
	 * @since 0.11.0
152
	 *
153
	 * @param ValidatorFactory $factory
154
	 * @return self
155
	 */
156
	public function validate( ValidatorFactory $factory ) {
157
		$rule_collection = $this->utilize('rules');
158
		$rules = $rule_collection->get_array( true );
159
160
		foreach( $rules as $field => $ruleset ) {
161
			$input = $this->field_value( $field );
162
			$optional = $rule_collection->is_optional( $field );
163
164
			foreach( $ruleset as $index => $rule ) {
165
				$params = $rule_collection->get_params( $field, $index );
166
				$this->validator->validate( $factory->get( $rule ), $field, $input, $optional, $params );
0 ignored issues
show
Bug introduced by
It seems like $factory->get($rule) targeting WFV\Factory\ValidatorFactory::get() can also be of type boolean; however, WFV\Validator::validate() does only seem to accept object<WFV\Contract\ValidateInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
167
			}
168
		}
169
		return $this;
170
	}
171
172
	/**
173
	 * Returns the input value for a field
174
	 * When not present, returns null
175
	 *
176
	 * @since 0.11.0
177
	 * @access protected
178
	 *
179
	 * @param string $field
180
	 */
181
	protected function field_value( $field ) {
182
		$input = $this->utilize('input');
183
		if( $input->has( $field ) ) {
184
			$input = $input->get_array( false );
185
			return $input[ $field ];
186
		}
187
		return null;
188
	}
189
190
	/**
191
	 *
192
	 *
193
	 * @since 0.10.0
194
	 * @access protected
195
	 *
196
	 * @param string $response
197
	 * @param string (optional) $field
198
	 * @param string (optional) $value
199
	 * @return string|null
200
	 */
201
	protected function string_or_null( $response, $field = null, $value = null ) {
202
		return ( $this->input( $field )->contains( $field, $value ) ) ? $response : null;
0 ignored issues
show
Unused Code introduced by
The call to FormComposite::input() has too many arguments starting with $field.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
203
	}
204
205
	/**
206
	 * Trigger action hook for validation pass or fail
207
	 *
208
	 * @since 0.10.0
209
	 * @access protected
210
	 *
211
	 * @param bool $is_valid
212
	 */
213
	protected function trigger_post_validate_action( $is_valid = false ) {
214
		$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail';
215
		do_action( $action, $this );
216
	}
217
}
218