Completed
Push — master ( 361a80...57c765 )
by Atanas
07:10
created

Select_Field::get_formatted_value()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 8
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
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 ) {
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
		if ( empty( $options_values ) ) {
56
			$options_values[] = '';
57
		}
58
59
		$value = $this->get_value();
60
		$value = $this->get_values_from_options( array( $value ) );
61
		$value = ! empty( $value ) ? $value[0] : $options_values[0];
62
63
		return $value;
64
	}
65
}
66