Completed
Pull Request — master (#193)
by
unknown
02:22
created

Complex_Value_Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 0 29 7
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 = array();
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
}