Completed
Push — master ( 5d138e...ae38d0 )
by Jamie
03:41
created

FrmFieldValueSelector::has_options()   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
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
92
		if ( $this->has_db_row() ) {
93
			$this->set_field_key();
94
			$this->set_field_settings();
95
			$this->set_options();
96
		}
97
	}
98
99
	/**
100
	 * Set the db_row property
101
	 *
102
	 * @since 2.03.05
103
	 */
104
	private function set_db_row() {
105
		$where = array(
106
			'id' => $this->field_id,
107
		);
108
109
		$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...
110
111
		if ( ! is_object( $this->db_row ) ) {
112
			$this->db_row = null;
113
		}
114
	}
115
116
	/**
117
	 * Set the field_key property
118
	 *
119
	 * @since 2.03.05
120
	 */
121
	private function set_field_key() {
122
		$this->field_key = $this->db_row->field_key;
123
	}
124
125
	/**
126
	 * Set the field_settings property
127
	 *
128
	 * @since 2.03.05
129
	 */
130
	protected function set_field_settings() {
131
		// Leave as null for free version
132
	}
133
134
	/**
135
	 * Set the options property
136
	 *
137
	 * @since 2.03.05
138
	 */
139
	protected function set_options() {
140
		$this->options = maybe_unserialize( $this->db_row->options );
141
	}
142
143
	/**
144
	 * Set the html_name property
145
	 *
146
	 * @since 2.03.05
147
	 *
148
	 * @param array $args
149
	 */
150
	protected function set_html_name( $args ) {
151
		if ( isset( $args['html_name'] ) ) {
152
			$this->html_name = (string) $args['html_name'];
153
		}
154
	}
155
156
	/**
157
	 * Set the selected_value property
158
	 *
159
	 * @since 2.03.05
160
	 *
161
	 * @param array $args
162
	 */
163
	protected function set_value( $args ) {
164
		if ( isset( $args['value'] ) ) {
165
			$this->value = (string) $args['value'];
166
		}
167
	}
168
169
	/**
170
	 * Set the source property
171
	 *
172
	 * @since 2.03.05
173
	 *
174
	 * @param array $args
175
	 */
176
	protected function set_source( $args ) {
177
		if ( isset( $args['source'] ) ) {
178
			$this->source = (string) $args['source'];
179
		}
180
	}
181
182
	/**
183
	 * Check if object has any options
184
	 *
185
	 * @since 2.03.05
186
	 *
187
	 * @return bool
188
	 */
189
	final protected function has_options() {
190
		return ! empty( $this->options );
191
	}
192
193
	/**
194
	 * Check if a field is connected to the value selector
195
	 *
196
	 * @since 2.03.05
197
	 *
198
	 * @return bool
199
	 */
200
	final protected function has_db_row() {
201
		return $this->db_row !== null;
202
	}
203
204
	/**
205
	 * Display the field value selector (dropdown or text field)
206
	 *
207
	 * @since 2.03.05
208
	 */
209
	public function display() {
210
		if ( $this->has_options() ) {
211
			$this->display_dropdown();
212
		} else {
213
			$this->display_text_box();
214
		}
215
	}
216
217
	/**
218
	 * Print the field value text box
219
	 *
220
	 * @since 2.03.05
221
	 */
222
	public function display_text_box() {
223
		echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( $this->value ) . '" />';
224
	}
225
226
	/**
227
	 * Display the field value selector
228
	 *
229
	 * @since 2.03.05
230
	 */
231
	protected function display_dropdown() {
232
		echo '<select name="' . esc_attr( $this->html_name ) . '">';
233
		echo '<option value="">' . esc_attr( $this->blank_option_label ) . '</option>';
234
235
		if ( ! empty( $this->options ) ) {
236
			foreach ( $this->options as $key => $value ) {
237
				if ( $value == '' ) {
238
					continue;
239
				}
240
241
				$option = $this->get_single_field_option( $key, $value );
242
				$option->print_single_option( $this->value, 25 );
243
			}
244
		}
245
246
		echo '</select>';
247
	}
248
249
	/**
250
	 * Get an instance of FrmFieldOption
251
	 *
252
	 * @since 2.03.05
253
	 *
254
	 * @param string $key
255
	 * @param string $value
256
	 *
257
	 * @return FrmFieldOption
258
	 */
259
	protected function get_single_field_option( $key, $value ) {
260
		return new FrmFieldOption( $key, $value );
261
	}
262
}