Completed
Pull Request — development (#586)
by
unknown
02:25
created

Multiselect_Field::to_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Helper\Delimiter;
6
use Carbon_Fields\Helper\Helper;
7
use Carbon_Fields\Value_Set\Value_Set;
8
9
/**
10
 * Multiselect field class.
11
 * Allows to create a select where multiple values can be selected.
12
 */
13
class Multiselect_Field extends Predefined_Options_Field {
14
	/**
15
	 * Default field value
16
	 *
17
	 * @var array
18
	 */
19
	protected $default_value = array();
20
21
	/**
22
	 * Value delimiter
23
	 *
24
	 * @var string
25
	 */
26
	protected $value_delimiter = '|';
27
28
	/**
29
	 * Create a field from a certain type with the specified label.
30
	 *
31
	 * @param string $type  Field type
32
	 * @param string $name  Field name
33
	 * @param string $label Field label
34
	 */
35
	public function __construct( $type, $name, $label ) {
36
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
37
		parent::__construct( $type, $name, $label );
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function set_value_from_input( $input ) {
44
		if ( ! isset( $input[ $this->get_name() ] ) ) {
45
			return $this->set_value( array() );
46
		}
47
48
		$value_delimiter = $this->value_delimiter;
49
		$options_values  = $this->get_options_values();
50
51
		$value = stripslashes_deep( $input[ $this->get_name() ] );
52
		$value = Delimiter::split( $value, $this->value_delimiter );
53
		$value = array_map(
54
			function( $val ) use ( $value_delimiter ) {
55
					return Delimiter::unquote( $val, $value_delimiter );
56
			},
57
			$value
58
		);
59
		$value = Helper::get_valid_options( $value, $options_values );
60
61
		return $this->set_value( $value );
62
	}
63
64
	public function quote_options_array( $option ) {
65
		if ( isset( $option['options'] ) ) {
66
			$option['options'] = array_map( array( $this, 'quote_options_array' ), $option['options'] );
67
		} else {
68
			$option['value'] = Delimiter::quote( $option['value'], $this->value_delimiter );
69
		}
70
71
		return $option;
72
	}
73
74
	/**
75
	 * {@inheritDoc}
76
	 */
77
	public function to_json( $load ) {
78
		$field_data = parent::to_json( $load );
79
80
		$value_delimiter = $this->value_delimiter;
81
82
		$options = $this->parse_options( $this->get_options(), true );
83
		$options = array_map( array( $this, 'quote_options_array' ), $options );
84
85
		$value = array_map(
86
			function( $value ) use ( $value_delimiter ) {
87
					return Delimiter::quote( $value, $value_delimiter );
88
			},
89
			$this->get_formatted_value()
90
		);
91
92
		$field_data = array_merge(
93
			$field_data,
94
			array(
95
				'options'        => $options,
96
				'value'          => $value,
97
				'valueDelimiter' => $this->value_delimiter,
98
			)
99
		);
100
101
		return $field_data;
102
	}
103
104
	/**
105
	 * {@inheritDoc}
106
	 */
107
	public function get_formatted_value() {
108
		$value = $this->get_value();
109
		$value = $this->get_values_from_options( $value );
110
		return $value;
111
	}
112
}
113