Completed
Push — development ( e96e62...aa0601 )
by
unknown
03:36
created

Multiselect_Field::to_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 23
rs 9.0856
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
	 * Load the field value from an input array based on its name
42
	 *
43
	 * @param  array $input Array of field names and values.
44
	 * @return self  $this
45
	 */
46
	public function set_value_from_input( $input ) {
47
		if ( ! isset( $input[ $this->name ] ) ) {
48
			return $this->set_value( array() );
49
		}
50
51
		$value_delimiter = $this->value_delimiter;
52
53
		$options = $this->parse_options( $this->get_options() );
54
		$options = wp_list_pluck( $options, 'value' );
55
56
		$value = stripslashes_deep( $input[ $this->name ] );
57
		$value = Delimiter::split( $value, $this->value_delimiter );
58
		$value = array_map( function( $val ) use ( $value_delimiter ) {
59
			return Delimiter::unquote( $val, $value_delimiter );
60
		}, $value );
61
		$value = Helper::get_valid_options( $value, $options );
62
63
		return $this->set_value( $value );
64
	}
65
66
	/**
67
	 * Returns an array that holds the field data, suitable for JSON representation.
68
	 *
69
	 * @param  bool  $load Should the value be loaded from the database or use the value from the current instance.
70
	 * @return array
71
	 */
72
	public function to_json( $load ) {
73
		$field_data = parent::to_json( $load );
74
75
		$value_delimiter = $this->value_delimiter;
76
77
		$options = $this->parse_options( $this->get_options() );
78
		$options = array_map( function( $option ) use ( $value_delimiter ) {
79
			$option['value'] = Delimiter::quote( $option['value'], $value_delimiter );
80
			return $option;
81
		}, $options );
82
83
		$value = array_map( function( $value ) use ( $value_delimiter ) {
84
			return Delimiter::quote( $value, $value_delimiter );
85
		}, $this->get_formatted_value() );
86
87
		$field_data = array_merge( $field_data, array(
88
			'options' => $options,
89
			'value' => $value,
90
			'valueDelimiter' => $this->value_delimiter,
91
		) );
92
93
		return $field_data;
94
	}
95
}
96