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 field options (if any) |
14
|
|
|
* |
15
|
|
|
* @var array|callable |
16
|
|
|
**/ |
17
|
|
|
protected $options = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the options of this field. |
21
|
|
|
* Accepts either array of data or a callback that returns the data. |
22
|
|
|
* |
23
|
|
|
* @param array|callable $options |
24
|
|
|
*/ |
25
|
9 |
View Code Duplication |
public function set_options( $options ) { |
|
|
|
|
26
|
9 |
|
$this->options = array(); |
27
|
|
|
|
28
|
9 |
|
if ( is_callable( $options ) ) { |
29
|
2 |
|
$this->options = $options; |
30
|
9 |
|
} elseif ( is_array( $options ) ) { |
31
|
3 |
|
$this->add_options( $options ); |
32
|
3 |
|
} else { |
33
|
4 |
|
$this->options = array(); |
34
|
4 |
|
Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>set_options()</code> method.' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add new options to this field. |
42
|
|
|
* Accepts an array of data. |
43
|
|
|
* |
44
|
|
|
* @param array|callable $options |
45
|
|
|
*/ |
46
|
9 |
View Code Duplication |
public function add_options( $options ) { |
|
|
|
|
47
|
9 |
|
if ( is_array( $options ) ) { |
48
|
4 |
|
$old_options = is_callable( $this->options ) ? array() : $this->options; |
49
|
4 |
|
$this->options = array_merge( $old_options, $options ); |
50
|
4 |
|
} else { |
51
|
5 |
|
$this->options = array(); |
52
|
5 |
|
Incorrect_Syntax_Exception::raise( 'Only arrays are allowed in the <code>add_options()</code> method.' ); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if there are callbacks and populate the options |
60
|
|
|
*/ |
61
|
|
|
protected function load_options() { |
62
|
|
|
if ( empty( $this->options ) ) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ( is_callable( $this->options ) ) { |
67
|
|
|
$options = call_user_func( $this->options ); |
68
|
|
|
if ( ! is_array( $options ) ) { |
69
|
|
|
$options = array(); |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$options = array(); |
73
|
|
|
foreach ( $this->options as $key => $value ) { |
74
|
|
|
if ( is_array( $value ) ) { |
75
|
|
|
$options = $options + $value; |
76
|
|
|
} else { |
77
|
|
|
$options[ $key ] = $value; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->options = $options; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
87
|
|
|
* Will also work with a callable that returns an array. |
88
|
|
|
* |
89
|
|
|
* @param array|callable $options |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function parse_options( $options ) { |
93
|
|
|
$parsed = array(); |
94
|
|
|
|
95
|
|
|
if ( is_callable( $options ) ) { |
96
|
|
|
$options = call_user_func( $options ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ( $options as $key => $value ) { |
100
|
|
|
$parsed[] = array( |
101
|
|
|
'name' => $value, |
102
|
|
|
'value' => $key, |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $parsed; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve the current options. |
111
|
|
|
* |
112
|
|
|
* @return array|callable $options |
113
|
|
|
*/ |
114
|
8 |
|
public function get_options() { |
115
|
8 |
|
return $this->options; |
116
|
|
|
} |
117
|
|
|
} // END Field |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.