Completed
Pull Request — master (#193)
by
unknown
09:54
created

Complex_Value_Parser::parse()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 11
nop 2
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php 
2
namespace Carbon_Fields\Updater;
3
4
use Carbon_Fields\Helper\Helper;
5
6
/**
7
* Class for parsing complex input
8
*/
9
class Complex_Value_Parser extends Value_Parser {
10
	
11
	/**
12
	 * Prepare $input for complex field
13
	 * 
14
	 * @param  array $input 
15
	 * @param  bool $is_option
16
	 * @return array $parsed_data
17
	 */
18
	public static function parse( $input, $is_option ) {
19
		if ( is_null( $input ) ) {
20
			return null;
21
		}
22
23
		if ( ! is_array( $input ) ) {
24
			self::throw_exception( __( 'Please provide an array or a json to be used for the update.', 'crb' ) );
25
		}
26
27
		if ( $is_option ) {
28
			return $input;
29
		}
30
31
		$parsed_data = [];
32
		
33
		foreach ( $input as $index => $group ) {
34
			foreach ( $group as $key => $value ) {
35
				$new_key = $key;
36
37
				if ( $key !== 'group' ) {
38
					$new_key = Helper::prepare_meta_name( $key );
39
				}
40
41
				$parsed_data[ $index ][ $new_key ] = $value;
42
			}
43
		}
44
45
		return $parsed_data;
46
	}
47
}