Completed
Push — development ( 84635d...987e6d )
by
unknown
21:46
created

Predefined_Options_Field::load_options()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 4
nop 0
dl 0
loc 23
ccs 0
cts 18
cp 0
crap 42
rs 8.5906
c 0
b 0
f 0
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
	/**
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
		if ( !is_callable( $options ) && !is_array( $options ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
27 4
			Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>set_options()</code> method.' );
28
		}
29
30 5
		$this->option_collections = array();
31 5
		return $this->add_options( $options );
32
	}
33
34
	/**
35
	 * Add new options to this field.
36
	 * Accepts either array of data or a callback that returns the data.
37
	 *
38
	 * @param array|callable $options
39
	 */
40 8 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...
41 8
		if ( !is_callable( $options ) && !is_array( $options ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
42 4
			Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>add_options()</code> method.' );
43
		}
44
45 4
		$this->option_collections[] = $options;
46 4
		return $this;
47
	}
48
49
	/**
50
	 * Check if there are callbacks and populate the options
51
	 */
52
	protected function load_options() {
53
		$options = array();
54
		foreach ( $this->option_collections as $collection ) {
55
			$collection_items = array();
56
			if ( is_callable( $collection ) ) {
57
				$collection_items = call_user_func( $collection );
58
				if ( !is_array( $collection_items ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
59
					continue;
60
				}
61
			} else {
62
				foreach ( $collection as $key => $value ) {
63
					if ( is_array( $value ) ) {
64
						$collection_items = $collection_items + $value;
65
					} else {
66
						$collection_items[ $key ] = $value;
67
					}
68
				}
69
			}
70
			$options = array_merge( $options, $collection_items );
71
		}
72
73
		return $options;
74
	}
75
76
	/**
77
	 * Retrieve the current options.
78
	 *
79
	 * @return array|callable $options
80
	 */
81 8
	public function get_options() {
82 8
		return $this->load_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
} // END Field
109