Completed
Pull Request — master (#77)
by
unknown
02:25
created

Predefined_Options_Field   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 112
ccs 17
cts 17
cp 1
rs 10
c 2
b 2
f 0
wmc 17
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_options() 0 3 1
A set_options() 0 14 3
A add_options() 0 16 4
B load_options() 0 23 6
A parse_options() 0 16 3
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 7
	public function set_options( $options ) {
26 7
		$this->options = array();
27
28
		if ( is_callable( $options ) ) {
29 2
			$this->options = $options;
30
		} elseif ( is_array( $options ) ) {
31
			$this->add_options( $options );
32
		} 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 3
		}
36
37 3
		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 5
	public function add_options( $options ) {
47
		if ( is_array( $options ) ) {
48
			$old_options = is_callable( $this->options ) ? array() : $this->options;
49
50 2
			if ( ! empty( $old_options ) ) {
51
				$this->options = array_merge( $old_options, $options );
52
			} else {
53 4
				$this->options = $options;
54 2
			}
55
		} else {
56 5
			$this->options = array();
57 5
			Incorrect_Syntax_Exception::raise( 'Only arrays are allowed in the <code>add_options()</code> method.' );
58 2
		}
59
60 2
		return $this;
61
	}
62
63
	/**
64
	 * Check if there are callbacks and populate the options
65
	 */
66
	protected function load_options() {
67
		if ( empty( $this->options ) ) {
68
			return false;
69
		}
70
71
		if ( is_callable( $this->options ) ) {
72
			$options = call_user_func( $this->options );
73
			if ( ! is_array( $options ) ) {
74
				$options = array();
75
			}
76
		} else {
77
			$options = array();
78
			foreach ( $this->options as $key => $value ) {
79
				if ( is_array( $value ) ) {
80
					$options = $options + $value;
81
				} else {
82
					$options[ $key ] = $value;
83
				}
84
			}
85
		}
86
87
		$this->options = $options;
88
	}
89
90
	/**
91
	 * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded.
92
	 * Will also work with a callable that returns an array.
93
	 *
94
	 * @param array|callable $options
95
	 * @return array
96
	 */
97
	public function parse_options( $options ) {
98
		$parsed = array();
99
100
		if ( is_callable( $options ) ) {
101
			$options = call_user_func( $options );
102
		}
103
104
		foreach ( $options as $key => $value ) {
105
			$parsed[] = array(
106
				'name' => $value,
107
				'value' => $key,
108
			);
109
		}
110
111
		return $parsed;
112
	}
113
114
	/**
115
	 * Retrieve the current options.
116
	 *
117
	 * @return array|callable $options
118
	 */
119 8
	public function get_options() {
120 8
		return $this->options;
121
	}
122
} // END Field
123