1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container\Fulfillable\Translator; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\App; |
6
|
|
|
use Carbon_Fields\Container\Condition\Factory; |
7
|
|
|
use Carbon_Fields\Container\Fulfillable\Fulfillable; |
8
|
|
|
use Carbon_Fields\Container\Fulfillable\Fulfillable_Collection; |
9
|
|
|
use Carbon_Fields\Container\Condition\Condition; |
10
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
11
|
|
|
|
12
|
|
|
class Array_Translator extends Translator { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Condition factory used to translated condition types |
16
|
|
|
* |
17
|
|
|
* @var Factory |
18
|
|
|
*/ |
19
|
|
|
protected $condition_factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param Factory $condition_factory |
25
|
|
|
*/ |
26
|
|
|
public function __construct( Factory $condition_factory ) { |
27
|
|
|
$this->condition_factory = $condition_factory; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Translate a Condition |
32
|
|
|
* |
33
|
|
|
* @param Condition $condition |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
protected function condition_to_foreign( Condition $condition ) { |
37
|
|
|
return array( |
38
|
|
|
'type' => $this->condition_factory->get_type( get_class( $condition ) ), |
39
|
|
|
'compare' => $condition->get_comparison_operator(), |
40
|
|
|
'value' => $condition->get_value(), |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Translate a Fulfillable_Collection |
46
|
|
|
* |
47
|
|
|
* @param Fulfillable_Collection $fulfillable_collection |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
protected function fulfillable_collection_to_foreign( Fulfillable_Collection $fulfillable_collection ) { |
51
|
|
|
$fulfillables = $fulfillable_collection->get_fulfillables(); |
52
|
|
|
if ( empty( $fulfillables ) ) { |
53
|
|
|
return array(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$collection = array( |
57
|
|
|
'relation' => 'AND', |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$relations = array(); |
61
|
|
|
foreach ( $fulfillables as $fulfillable_tuple ) { |
62
|
|
|
$comparison = $fulfillable_tuple['fulfillable_comparison']; |
63
|
|
|
$fulfillable = $fulfillable_tuple['fulfillable']; |
64
|
|
|
|
65
|
|
|
if ( ! isset( $relations[ $comparison ] ) ) { |
66
|
|
|
$relations[ $comparison ] = array(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$relations[ $comparison ][] = $this->fulfillable_to_foreign( $fulfillable ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ( ! empty( $relations['OR'] ) ) { |
73
|
|
|
$collection['relation'] = 'OR'; |
74
|
|
|
} |
75
|
|
|
foreach ( $relations as $relation => $fulfillables ) { |
76
|
|
|
$collection[] = array( 'relation' => $relation ) + $fulfillables; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ( count( $relations ) === 1 ) { |
80
|
|
|
// we only have one relation group so we simplify the fulfillables with 1 level |
81
|
|
|
$collection = $collection[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return array_filter( $collection ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Translate foreign data to a Fulfillable |
89
|
|
|
* |
90
|
|
|
* @param mixed $foreign |
91
|
|
|
* @return Fulfillable |
92
|
|
|
*/ |
93
|
1 |
|
public function foreign_to_fulfillable( $foreign ) { |
94
|
|
|
if ( ! is_array( $foreign ) ) { |
95
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid data passed to array condition translator: ' . print_r( $foreign, true ) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
if ( isset( $foreign['type'] ) ) { |
99
|
|
|
return $this->foreign_to_native_condition( $foreign ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
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 = App::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
|
|
|
|