Completed
Push — master ( e0ab63...e0ab63 )
by Maciej
10s
created

FormComposite::checked_if()   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 2
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 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.10.0
38
	 *
39
	 * @param ArtisanInterface $builder
40
	 */
41
	public function __construct( FormArtisan $builder ) {
42
		$this->alias = $builder->action;
43
		$this->collection = $builder->collection;
44
	}
45
46
	/**
47
	 * Convenience method to repopulate checkbox input
48
	 *
49
	 * @since 0.10.0
50
	 *
51
	 * @param string $field Field name.
52
	 * @param string $value Value to compare against.
53
	 * @return string|null
54
	 */
55
	public function checked_if( $field = null, $value = null ) {
56
		return $this->string_or_null( 'checked', $field, $value );
57
	}
58
59
	/**
60
	 * Echo the encoded value of given field from a callback
61
	 * Default callback is esc_html()
62
	 * Also returns the encoded string for assignment
63
	 *
64
	 * @since 0.10.1
65
	 *
66
	 * @param string (optional) $field
67
	 * @param callable (optional) $callback
68
	 * @return string
69
	 */
70
	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...
71
		echo $input = $this->utilize('input')->escape( $field );
72
		return $input;
73
	}
74
75
	/**
76
	 * Use error collection
77
	 *
78
	 *
79
	 * @since 0.10.0
80
	 *
81
	 * @return WFV\Collection\ErrorCollection
82
	 */
83
	public function errors() {
84
		return $this->utilize('errors');
85
	}
86
87
	/**
88
	 * Convenience method to check if form has input errors
89
	 *
90
	 *
91
	 * @since 0.12.0
92
	 *
93
	 * @return bool
94
	 */
95
	public function has_errors() {
96
		return $this->utilize('errors')->is_populated();
97
	}
98
99
	/**
100
	 * Use input collection
101
	 *
102
	 * @since 0.10.0
103
	 *
104
	 * @return WFV\Collection\InputCollection
105
	 */
106
	public function input() {
107
		return $this->utilize('input');
108
	}
109
110
	/**
111
	 * Convenience method to check if validation passed
112
	 *
113
	 * @since 0.12.0
114
	 *
115
	 * @return bool
116
	 */
117
	public function is_valid() {
118
		return ( $this->input()->is_populated() ) &&
119
			!$this->has_errors();
120
	}
121
122
	/**
123
	 * Return the form name/action
124
	 *
125
	 * @since 0.11.3
126
	 *
127
	 * @return string
128
	 */
129
	public function name() {
130
		return $this->alias;
131
	}
132
133
	/**
134
	 * Use rules collection
135
	 *
136
	 * @since 0.11.0
137
	 *
138
	 * @return WFV\Collection\RuleCollection
139
	 */
140
	public function rules() {
141
		return $this->utilize('rules');
142
	}
143
144
	/**
145
	 * Convenience method to repopulate select input
146
	 *
147
	 * @since 0.10.0
148
	 *
149
	 * @param string $field Field name.
150
	 * @param string $value Value to compare against.
151
	 * @return string|null
152
	 */
153
	public function selected_if( $field = null, $value = null ) {
154
		return $this->string_or_null( 'selected', $field, $value );
155
	}
156
157
	/**
158
	 * Convienience method to print the hidden fields
159
	 *  for token and action
160
	 *
161
	 * @since 0.10.0
162
	 *
163
	 */
164
	public function token_fields() {
165
		// TODO - Move markup into something - perhaps a renderable interface?
166
		$token_name = $this->alias . '_token';
167
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
168
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
169
	}
170
171
	/**
172
	 *
173
	 *
174
	 * @since 0.10.0
175
	 * @access protected
176
	 *
177
	 * @param string $response
178
	 * @param string (optional) $field
179
	 * @param string (optional) $value
180
	 * @return string|null
181
	 */
182
	protected function string_or_null( $response, $field = null, $value = null ) {
183
		$input = $this->utilize('input');
184
		return ( $input->contains( $field, $value ) ) ? $response : null;
185
	}
186
187
	/**
188
	 * Use a component.
189
	 *
190
	 * @since 0.10.0
191
	 * @access protected
192
	 *
193
	 * @param string $component Key indentifier.
194
	 */
195
	protected function utilize( $component ) {
196
		return $this->collection[ $component ];
197
	}
198
}
199