Completed
Push — master ( 1a45bc...c7ca0b )
by Marin
02:44
created

Predefined_Options_Field::add_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 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 9 View Code Duplication
	public function set_options( $options ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
	 *
88
	 * @param array $options
89
	 * @return array
90
	 */
91
	public function parse_options( $options ) {
92
		$parsed = array();
93
94
		foreach ( $options as $key => $value ) {
95
			$parsed[] = array(
96
				'name' => $value,
97
				'value' => $key,
98
			);
99
		}
100
101
		return $parsed;
102
	}
103
104
	/**
105
	 * Retrieve the current options.
106
	 *
107
	 * @return array|callable $options 
108
	 */
109 8
	public function get_options() {
110 8
		return $this->options;
111
	}
112
113
} // END Field
114