|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Base class for fields with predefined options. |
|
9
|
|
|
* Mainly used to reduce the bloat on the base Field class. |
|
10
|
|
|
**/ |
|
11
|
|
|
abstract class Predefined_Options_Field extends Field { |
|
12
|
|
|
/** |
|
13
|
|
|
* Stores the raw, unprocessed field options |
|
14
|
|
|
* |
|
15
|
|
|
* @var array(array|callable) |
|
16
|
|
|
**/ |
|
17
|
|
|
protected $option_collections = array(); |
|
18
|
|
|
|
|
19
|
|
|
protected function is_indexed_array( $array ) { |
|
20
|
|
|
return array_keys( $array ) === range( 0, count( $array ) - 1 ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set the options of this field. |
|
25
|
|
|
* Accepts either array of data or a callback that returns the data. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array|callable $options |
|
28
|
|
|
*/ |
|
29
|
9 |
View Code Duplication |
public function set_options( $options ) { |
|
30
|
9 |
|
if ( ! is_callable( $options ) && ! is_array( $options ) ) { |
|
31
|
4 |
|
Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>set_options()</code> method.' ); |
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
$this->option_collections = array(); |
|
36
|
5 |
|
return $this->add_options( $options ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add new options to this field. |
|
41
|
|
|
* Accepts either array of data or a callback that returns the data. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array|callable $options |
|
44
|
|
|
*/ |
|
45
|
13 |
View Code Duplication |
public function add_options( $options ) { |
|
46
|
13 |
|
if ( ! is_callable( $options ) && ! is_array( $options ) ) { |
|
47
|
4 |
|
Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>add_options()</code> method.' ); |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
$this->option_collections[] = $options; |
|
52
|
9 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if there are callbacks and populate the options |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function load_options() { |
|
59
|
|
|
$options = array(); |
|
60
|
|
|
foreach ( $this->option_collections as $collection ) { |
|
61
|
|
|
$collection_items = array(); |
|
62
|
|
|
if ( is_callable( $collection ) ) { |
|
63
|
|
|
$collection_items = call_user_func( $collection ); |
|
64
|
|
|
if ( ! is_array( $collection_items ) ) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
foreach ( $collection as $key => $value ) { |
|
69
|
|
|
if ( is_array( $value ) ) { |
|
70
|
|
|
$collection_items = $collection_items + $value; |
|
71
|
|
|
} else { |
|
72
|
|
|
$collection_items[ $key ] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
if ( $this->is_indexed_array( $options ) && $this->is_indexed_array( $collection_items ) ) { |
|
77
|
|
|
$options = array_merge( $options, $collection_items ); |
|
78
|
|
|
} else { |
|
79
|
|
|
$options = array_replace( $options, $collection_items ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $options; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve the current options. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array|callable $options |
|
90
|
|
|
*/ |
|
91
|
13 |
|
public function get_options() { |
|
92
|
13 |
|
return $this->load_options(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
|
97
|
|
|
* Will also work with a callable that returns an array. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array|callable $options |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function parse_options( $options ) { |
|
103
|
|
|
$parsed = array(); |
|
104
|
|
|
|
|
105
|
|
|
if ( is_callable( $options ) ) { |
|
106
|
|
|
$options = call_user_func( $options ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
foreach ( $options as $key => $value ) { |
|
110
|
|
|
$parsed[] = array( |
|
111
|
|
|
'name' => $value, |
|
112
|
|
|
'value' => $key, |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $parsed; |
|
117
|
|
|
} |
|
118
|
|
|
} // END Field |
|
119
|
|
|
|