Json_Translator::foreign_to_json()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 1
dl 0
loc 35
ccs 0
cts 22
cp 0
crap 56
rs 8.4266
c 0
b 0
f 0
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
				if ( isset( $value['type'] ) ) {
46
					$conditions[] = $value;
47
				} else {
48
					$conditions = array_merge( $conditions, $value );
49
				}
50
			}
51
		}
52
53
		return array(
54
			'relation' => $foreign['relation'],
55
			'conditions' => $conditions,
56
		);
57
	}
58
}
59