Completed
Push — development ( 140d24...d693da )
by
unknown
02:48
created

Select_Field::get_formatted_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 9.6666
ccs 0
cts 6
cp 0
crap 6
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Helper\Helper;
6
7
/**
8
 * Select dropdown field class.
9
 */
10
class Select_Field extends Predefined_Options_Field {
11
	/**
12
	 * {@inheritDoc}
13
	 */
14
	public function set_value_from_input( $input ) {
15
		$options_values = $this->get_options_values();
16
17
		$value = null;
18
		if ( isset( $input[ $this->get_name() ] ) ) {
19
			$raw_value = stripslashes_deep( $input[ $this->get_name() ] );
20
			$raw_value = Helper::get_valid_options( array( $raw_value ), $options_values );
21
			if ( ! empty( $raw_value ) ) {
22
				$value = $raw_value[0];
23
			}
24
		}
25
26
		if ( $value === null ) {
27
			$value = $options_values[0];
28
		}
29
30
		return $this->set_value( $value );
31
	}
32
33
	/**
34
	 * {@inheritDoc}
35
	 */
36 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
		$field_data = parent::to_json( $load );
38
39
		$options = $this->parse_options( $this->get_options(), true );
40
		$value = strval( $this->get_formatted_value() );
41
42
		$field_data = array_merge( $field_data, array(
43
			'value' => strval( $value ),
44
			'options' => $options,
45
		) );
46
47
		return $field_data;
48
	}
49
50
	/**
51
	 * {@inheritDoc}
52
	 */
53
	public function get_formatted_value() {
54
		$options_values = $this->get_options_values();
55
56
		$value = $this->get_value();
57
		$value = $this->get_values_from_options( array( $value ) );
58
		$value = ! empty( $value ) ? $value[0] : $options_values[0];
59
60
		return $value;
61
	}
62
}
63