Completed
Push — milestone/2_0/react-ui ( 50361a...b4837a )
by
unknown
03:06
created

Date_Field::set_value_from_input()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Date picker field class.
7
 */
8
class Date_Field extends Field {
9
10
	/**
11
	 * The storage format for use in PHP
12
	 *
13
	 * @var string
14
	 */
15
	protected $storage_format = 'Y-m-d';
16
17
	/**
18
	 * The expected input format for use in PHP
19
	 *
20
	 * @var string
21
	 */
22
	protected $input_format_php = 'Y-m-d';
23
24
	/**
25
	 * The expected input format for use in Flatpickr JS
26
	 *
27
	 * @var string
28
	 */
29
	protected $input_format_js = 'Y-m-d';
30
31
	/**
32
	 * Picker options.
33
	 *
34
	 * @var array
35
	 */
36
	protected $picker_options = array(
37
		'allowInput' => true,
38
	);
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function set_value_from_input( $input ) {
44
		if ( isset( $input[ $this->get_name() ] ) ) {
45
			$date = DateTime::createFromFormat( $this->input_format_php, $input[ $this->get_name() ] );
46
			$value = ( $date !== false ) ? $date->format( $this->storage_format ) : '';
47
			$this->set_value( $value );
48
		} else {
49
			$this->clear_value();
50
		}
51
	}
52
53
	/**
54
	 * {@inheritDoc}
55
	 */
56
	public function to_json( $load ) {
57
		$field_data = parent::to_json( $load );
58
59
		$field_data = array_merge( $field_data, array(
60
			'storage_format' => $this->get_storage_format(),
61
			'picker_options' => array_merge( $this->get_picker_options(), array(
62
				'dateFormat' => $this->input_format_js,
63
			) ),
64
		) );
65
66
		return $field_data;
67
	}
68
69
	/**
70
	 * Get storage format
71
	 *
72
	 * @return string
73
	 */
74
	public function get_storage_format() {
75
		return $this->storage_format;
76
	}
77
78
	/**
79
	 * Set storage format
80
	 *
81
	 * @param  string $storage_format
82
	 * @return Field  $this
83
	 */
84
	public function set_storage_format( $storage_format ) {
85
		$this->storage_format = $storage_format;
86
		return $this;
87
	}
88
89
	/**
90
	 * Get the expected input format in php and js variants
91
	 * 
92
	 * @return array
93
	 */
94
	public function get_input_format( $php_format, $js_format ) {
95
		$this->input_format_php = $php_format;
96
		$this->input_format_js = $js_format;
97
		return $this;
98
	}
99
100
	/**
101
	 * Set a format for use on the front-end in both PHP and Flatpickr formats
102
	 * The formats should produce identical results (i.e. they are translations of each other)
103
	 * 
104
	 * @param  string $php_format
105
	 * @param  string $js_format
106
	 * @return Field  $this
107
	 */
108
	public function set_input_format( $php_format, $js_format ) {
109
		$this->input_format_php = $php_format;
110
		$this->input_format_js = $js_format;
111
		return $this;
112
	}
113
114
	/**
115
	 * Returns the picker options.
116
	 * 
117
	 * @return array
118
	 */
119
	public function get_picker_options() {
120
		return $this->picker_options;
121
	}
122
123
	/**
124
	 * Set datepicker options
125
	 * 
126
	 * @param  array $options
127
	 * @return Field $this
128
	 */
129
	public function set_picker_options( $options ) {
130
		$this->picker_options = array_replace( $this->picker_options, $options );
131
		return $this;
132
	}
133
}
134