Completed
Push — master ( cb49b4...0fd3b2 )
by Maciej
02:19
created

FormComposite::token_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
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.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
	 * Use input collection
89
	 *
90
	 * @since 0.10.0
91
	 *
92
	 * @return WFV\Collection\InputCollection
93
	 */
94
	public function input() {
95
		return $this->utilize('input');
96
	}
97
98
	/**
99
	 * Return the form name/action
100
	 *
101
	 * @since 0.11.3
102
	 *
103
	 * @return string
104
	 */
105
	public function name() {
106
		return $this->alias;
107
	}
108
109
	/**
110
	 * Use rules collection
111
	 *
112
	 * @since 0.11.0
113
	 *
114
	 * @return WFV\Collection\RuleCollection
115
	 */
116
	public function rules() {
117
		return $this->utilize('rules');
118
	}
119
120
	/**
121
	 * Convenience method to repopulate select input
122
	 *
123
	 * @since 0.10.0
124
	 *
125
	 * @param string $field Field name.
126
	 * @param string $value Value to compare against.
127
	 * @return string|null
128
	 */
129
	public function selected_if( $field = null, $value = null ) {
130
		return $this->string_or_null( 'selected', $field, $value );
131
	}
132
133
	/**
134
	 * Convienience method to print the hidden fields
135
	 *  for token and action
136
	 *
137
	 * @since 0.10.0
138
	 *
139
	 */
140
	public function token_fields() {
141
		// TODO - Move markup into something - perhaps a renderable interface?
142
		$token_name = $this->alias . '_token';
143
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
144
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
145
	}
146
147
	/**
148
	 *
149
	 *
150
	 * @since 0.10.0
151
	 * @access protected
152
	 *
153
	 * @param string $response
154
	 * @param string (optional) $field
155
	 * @param string (optional) $value
156
	 * @return string|null
157
	 */
158
	protected function string_or_null( $response, $field = null, $value = null ) {
159
		$input = $this->utilize('input');
160
		return ( $input->contains( $field, $value ) ) ? $response : null;
161
	}
162
163
	/**
164
	 * Use a component.
165
	 *
166
	 * @since 0.10.0
167
	 * @access protected
168
	 *
169
	 * @param string $component Key indentifier.
170
	 */
171
	protected function utilize( $component ) {
172
		return $this->collection[ $component ];
173
	}
174
}
175