Completed
Push — milestone/2_0/react-ui ( 5b168c...b737dc )
by
unknown
04:15
created

Json_Translator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fulfillable_to_foreign() 0 4 1
B foreign_to_json() 0 31 6
1
<?php
2
3
namespace Carbon_Fields\Container\Fulfillable\Translator;
4
5
use Carbon_Fields\Container\Fulfillable\Fulfillable;
6
7
class Json_Translator extends Array_Translator {
8
9
	/**
10
	 * @inheritdoc
11
	 */
12
	public function fulfillable_to_foreign( Fulfillable $fulfillable ) {
13
		$result = parent::fulfillable_to_foreign( $fulfillable );
14
		return $this->foreign_to_json( $result );
15
	}
16
17
	/**
18
	 * Make conditions friendly for json-based frontend.
19
	 *
20
	 * @param  array $foreign
21
	 * @return array
22
	 */
23
	protected function foreign_to_json( $foreign ) {
24
		if ( empty( $foreign ) ) {
25
			return array(
26
				'relation' => 'AND',
27
				'conditions' => array(),
28
			);
29
		}
30
		
31
		if ( ! isset( $foreign['relation'] ) ) {
32
			return $foreign;
33
		}
34
35
		$conditions = array();
36
37
		foreach ( $foreign as $key => $value ) {
38
			if ( $key === 'relation' ) {
39
				continue;
40
			}
41
42
			if ( isset( $value['relation'] ) ) {
43
				$conditions[] = $this->foreign_to_json( $value );
44
			} else {
45
				$conditions[] = $value;
46
			}
47
		}
48
49
		return array(
50
			'relation' => $foreign['relation'],
51
			'conditions' => $conditions,
52
		);
53
	}
54
}
55