Completed
Pull Request — master (#54)
by Jamie
03:26
created

FrmFieldValueSelector::display_dropdown()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 0
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * A class for the field value selector
5
 * Used in field conditional logic, action conditional logic, MailChimp action, etc.
6
 *
7
 * @since 2.03.05
8
 */
9
class FrmFieldValueSelector {
10
11
	/**
12
	 * @var int
13
	 *
14
	 * @since 2.03.05
15
	 */
16
	protected $field_id = 0;
17
18
	/**
19
	 * @var string
20
	 *
21
	 * @since 2.03.05
22
	 */
23
	protected $field_key = '';
24
25
	/**
26
	 * @var null
27
	 *
28
	 * @since 2.03.05
29
	 */
30
	protected $field_settings = null;
31
32
	/**
33
	 * @var array
34
	 *
35
	 * @since 2.03.05
36
	 */
37
	protected $options = array();
38
39
40
	/**
41
	 * @var string
42
	 *
43
	 * @since 2.03.05
44
	 */
45
	protected $html_name = '';
46
47
	/**
48
	 * @var string
49
	 *
50
	 * @since 2.03.05
51
	 */
52
	protected $value = '';
53
54
	/**
55
	 * @var string
56
	 *
57
	 * @since 2.03.05
58
	 */
59
	protected $source = 'unknown';
60
61
	/**
62
	 * @var string
63
	 *
64
	 * @since 2.03.05
65
	 */
66
	protected $blank_option_label = '';
67
68
	/**
69
	 * @var object
70
	 *
71
	 * @since 2.03.05
72
	 */
73
	protected $db_row = null;
74
75
	/**
76
	 * FrmFieldValueSelector constructor
77
	 *
78
	 * @param int|string $field_id
79
	 */
80
	public function __construct( $field_id, $args ) {
81
		$this->set_html_name( $args );
82
		$this->set_value( $args );
83
		$this->set_source( $args );
84
85
		$this->field_id = (int) $field_id;
86
		if ( $this->field_id === 0 ) {
87
			return;
88
		}
89
90
		$this->set_db_row();
91
		$this->set_field_key();
92
		$this->set_field_settings();
93
		$this->set_options();
94
	}
95
96
	/**
97
	 * Set the db_row property
98
	 *
99
	 * @since 2.03.05
100
	 */
101
	private function set_db_row() {
102
		$where = array(
103
			'id' => $this->field_id,
104
		);
105
106
		$this->db_row = FrmDb::get_row( 'frm_fields', $where );
0 ignored issues
show
Documentation Bug introduced by
It seems like \FrmDb::get_row('frm_fields', $where) can also be of type array or string. However, the property $db_row is declared as type object. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
107
	}
108
109
	/**
110
	 * Set the field_key property
111
	 *
112
	 * @since 2.03.05
113
	 */
114
	private function set_field_key() {
115
		$this->field_key = $this->db_row->field_key;
116
	}
117
118
	/**
119
	 * Set the field_settings property
120
	 *
121
	 * @since 2.03.05
122
	 */
123
	protected function set_field_settings() {
124
		// Leave as null for free version
125
	}
126
127
	/**
128
	 * Set the options property
129
	 *
130
	 * @since 2.03.05
131
	 */
132
	protected function set_options() {
133
		$this->options = maybe_unserialize( $this->db_row->options );
134
	}
135
136
	/**
137
	 * Set the html_name property
138
	 *
139
	 * @since 2.03.05
140
	 *
141
	 * @param array $args
142
	 */
143
	protected function set_html_name( $args ) {
144
		if ( isset( $args['html_name'] ) ) {
145
			$this->html_name = (string) $args['html_name'];
146
		}
147
	}
148
149
	/**
150
	 * Set the selected_value property
151
	 *
152
	 * @since 2.03.05
153
	 *
154
	 * @param array $args
155
	 */
156
	protected function set_value( $args ) {
157
		if ( isset( $args['value'] ) ) {
158
			$this->value = (string) $args['value'];
159
		}
160
	}
161
162
	/**
163
	 * Set the source property
164
	 *
165
	 * @since 2.03.05
166
	 *
167
	 * @param array $args
168
	 */
169
	protected function set_source( $args ) {
170
		if ( isset( $args['source'] ) ) {
171
			$this->source = (string) $args['source'];
172
		}
173
	}
174
175
	/**
176
	 * Check if object has any options
177
	 *
178
	 * @since 2.03.05
179
	 *
180
	 * @return bool
181
	 */
182
	final function has_options() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
		return ! empty( $this->options );
184
	}
185
186
	/**
187
	 * Check if a field is connected to the value selector
188
	 *
189
	 * @since 2.03.05
190
	 *
191
	 * @return bool
192
	 */
193
	final function has_field() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
194
		return $this->field_id !== 0;
195
	}
196
197
	/**
198
	 * Display the field value selector (dropdown or text field)
199
	 *
200
	 * @since 2.03.05
201
	 */
202
	public function display() {
203
		if ( $this->has_options() ) {
204
			$this->display_dropdown();
205
		} else {
206
			$this->display_text_box();
207
		}
208
	}
209
210
	/**
211
	 * Print the field value text box
212
	 *
213
	 * @since 2.03.05
214
	 */
215
	public function display_text_box() {
216
		echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( $this->value ) . '" />';
217
	}
218
219
	/**
220
	 * Display the field value selector
221
	 *
222
	 * @since 2.03.05
223
	 */
224
	protected function display_dropdown() {
225
		echo '<select name="' . esc_attr( $this->html_name ) . '">';
226
		echo '<option value="">' . esc_attr( $this->blank_option_label ) . '</option>';
227
228
		if ( ! empty( $this->options ) ) {
229
			foreach ( $this->options as $key => $value ) {
230
				if ( $value == '' ) {
231
					continue;
232
				}
233
234
				$option = $this->get_single_field_option( $key, $value );
235
				$option->print_single_option( $this->value, 25 );
236
			}
237
		}
238
239
		echo '</select>';
240
	}
241
242
	/**
243
	 * Get an instance of FrmFieldOption
244
	 *
245
	 * @since 2.03.05
246
	 *
247
	 * @param string $key
248
	 * @param string $value
249
	 *
250
	 * @return FrmFieldOption
251
	 */
252
	protected function get_single_field_option( $key, $value ) {
253
		return new FrmFieldOption( $key, $value );
254
	}
255
}