Completed
Branch milestone/2_0/react-ui (57d10c)
by htmlBurger
02:47
created

Array_Translator   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 8.2%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 163
ccs 5
cts 61
cp 0.082
rs 10
wmc 24
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A foreign_to_native_condition() 0 10 3
A __construct() 0 3 1
A foreign_to_fulfillable() 0 12 3
A condition_to_foreign() 0 7 1
C fulfillable_collection_to_foreign() 0 36 7
A foreign_to_native_fulfillable_collection() 0 11 4
B foreign_to_json() 0 27 5
1
<?php
2
3
namespace Carbon_Fields\Container\Fulfillable\Translator;
4
5
use Carbon_Fields\Container\Condition\Factory;
6
use Carbon_Fields\Container\Fulfillable\Fulfillable;
7
use Carbon_Fields\Container\Fulfillable\Fulfillable_Collection;
8
use Carbon_Fields\Container\Condition\Condition;
9
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
10
11
class Array_Translator extends Translator {
12
13
	/**
14
	 * Condition factory used to translated condition types
15
	 *
16
	 * @var Factory
17
	 */
18
	protected $condition_factory;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param Factory $condition_factory
24
	 */
25
	public function __construct( Factory $condition_factory ) {
26
		$this->condition_factory = $condition_factory;
27
	}
28
29
	/**
30
	 * Translate a Condition
31
	 *
32
	 * @param  Condition $condition
33
	 * @return mixed
34
	 */
35
	protected function condition_to_foreign( Condition $condition ) {
36
		return array(
37
			'type' => $this->condition_factory->get_type( get_class( $condition ) ),
38
			'compare' => $condition->get_comparison_operator(),
39
			'value' => $condition->get_value(),
40
		);
41
	}
42
43
	/**
44
	 * Translate a Fulfillable_Collection
45
	 *
46
	 * @param  Fulfillable_Collection $fulfillable_collection
47
	 * @return mixed
48
	 */
49
	protected function fulfillable_collection_to_foreign( Fulfillable_Collection $fulfillable_collection ) {
50
		$fulfillables = $fulfillable_collection->get_fulfillables();
51
		if ( empty( $fulfillables ) ) {
52
			return array();
53
		}
54
55
		$collection = array(
56
			'relation' => 'AND',
57
		);
58
59
		$relations = array();
60
		foreach ( $fulfillables as $fulfillable_tuple ) {
61
			$comparison = $fulfillable_tuple['fulfillable_comparison'];
62
			$fulfillable = $fulfillable_tuple['fulfillable'];
63
64
			if ( ! isset( $relations[ $comparison ] ) ) {
65
				$relations[ $comparison ] = array();
66
			}
67
68
			$relations[ $comparison ][] = $this->fulfillable_to_foreign( $fulfillable );
69
		}
70
71
		if ( ! empty( $relations['OR'] ) ) {
72
			$collection['relation'] = 'OR';
73
		}
74
		foreach ( $relations as $relation => $fulfillables ) {
75
			$collection[] = array( 'relation' => $relation ) + $fulfillables;
76
		}
77
78
		if ( count( $relations ) === 1 ) {
79
			// we only have one relation group so we simplify the fulfillables with 1 level
80
			$collection = $collection[0];
81
		}
82
83
		return array_filter( $collection );
84
	}
85
86
	/**
87
	 * Translate foreign data to a Fulfillable
88
	 *
89
	 * @param  mixed       $foreign
90
	 * @return Fulfillable
91
	 */
92 3
	public function foreign_to_fulfillable( $foreign ) {
93 3
		if ( ! is_array( $foreign ) ) {
94
			Incorrect_Syntax_Exception::raise( 'Invalid data passed to array condition translator: ' . print_r( $foreign, true ) );
95
			return null;
96
		}
97
98 3
		if ( isset( $foreign['type'] ) ) {
99 3
			return $this->foreign_to_native_condition( $foreign );
100
		}
101
102 2
		return $this->foreign_to_native_fulfillable_collection( $foreign );
103
	}
104
105
	/**
106
	 * Translate a Condition
107
	 *
108
	 * @param  array     $foreign
109
	 * @return Condition
110
	 */
111
	protected function foreign_to_native_condition( $foreign ) {
112
		$condition_type = $foreign['type'];
113
		$comparison_operator = isset( $foreign['compare'] ) ? $foreign['compare'] : '=';
114
		$value = isset( $foreign['value'] ) ? $foreign['value'] : '';
115
116
		$condition = $this->condition_factory->make( $condition_type );
117
		$condition->set_comparison_operator( $comparison_operator );
118
		$condition->set_value( $value );
119
		return $condition;
120
	}
121
122
	/**
123
	 * Translate a Fulfillable_Collection
124
	 *
125
	 * @param  array                  $foreign
126
	 * @return Fulfillable_Collection
127
	 */
128
	protected function foreign_to_native_fulfillable_collection( $foreign ) {
129
		$fulfillable_comparison = isset( $foreign['relation'] ) ? $foreign['relation'] : 'AND';
130
		$collection = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
131
		foreach ( $foreign as $key => $value ) {
132
			if ( $key === 'relation' ) {
133
				continue; // ignore the relation key - we are only interested in condition definitions
134
			}
135
			$collection->add_fulfillable( $this->foreign_to_fulfillable( $value ), $fulfillable_comparison );
136
		}
137
		return $collection;
138
	}
139
140
	/**
141
	 * Make conditions friendly for frontend.
142
	 *
143
	 * @param  array $foreign
144
	 * @return array
145
	 */
146
	public function foreign_to_json( $foreign ) {
147
		if ( empty( $foreign ) ) {
148
			return array(
149
				'relation' => 'AND',
150
				'conditions' => array(),
151
			);
152
		}
153
154
		$conditions = array();
155
156
		foreach ( $foreign as $key => $value ) {
157
			if ( $key === 'relation' ) {
158
				continue;
159
			}
160
161
			if ( isset( $value['relation'] ) ) {
162
				$conditions[] = $this->foreign_to_json( $value );
163
			} else {
164
				$conditions[] = $value;
165
			}
166
		}
167
168
		return array(
169
			'relation' => $foreign['relation'],
170
			'conditions' => $conditions,
171
		);
172
	}
173
}
174