Completed
Push — master ( b351c8...f1e487 )
by
unknown
05:55
created

Select_Field   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 33
ccs 0
cts 13
cp 0
rs 10
c 2
b 2
f 0
wmc 3
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B to_json() 0 24 3
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Select dropdown field class.
7
 */
8
class Select_Field extends Predefined_Options_Field {
9
10
	/**
11
	 * Returns an array that holds the field data, suitable for JSON representation.
12
	 *
13
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
14
	 * @return array
15
	 */
16
	public function to_json( $load ) {
17
		$field_data = parent::to_json( $load );
18
19
		$options = $this->parse_options( $this->get_options() );
20
		$values = wp_list_pluck( $options, 'value' );
21
		$value = $this->get_formatted_value();
22
		if ( ! empty( $values ) ) {
23
			// this roundabout way is required in order to keep proper value types
24
			// as values taken from the database are always strings
25
			$value_index = array_search( $value, $values );
26
			if ( $value_index !== false ) {
27
				$value = $values[ $value_index ];
28
			} else {
29
				$value = $values[0];
30
			}
31
		}
32
33
		$field_data = array_merge( $field_data, array(
34
			'value' => $value,
35
			'options' => $options,
36
		) );
37
38
		return $field_data;
39
	}
40
}
41