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
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
protected function condition_to_foreign( Condition $condition ) { |
33
|
|
|
return array( |
34
|
|
|
'type' => $this->condition_factory->get_type( get_class( $condition ) ), |
35
|
|
|
'compare' => $condition->get_comparison_operator(), |
36
|
|
|
'value' => $condition->get_value(), |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
protected function fulfillable_collection_to_foreign( Fulfillable_Collection $fulfillable_collection ) { |
44
|
|
|
$fulfillables = $fulfillable_collection->get_fulfillables(); |
45
|
|
|
if ( empty( $fulfillables ) ) { |
46
|
|
|
return array(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$collection = array( |
50
|
|
|
'relation' => 'AND', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$relations = array(); |
54
|
|
|
foreach ( $fulfillables as $fulfillable_tuple ) { |
55
|
|
|
$comparison = $fulfillable_tuple['fulfillable_comparison']; |
56
|
|
|
$fulfillable = $fulfillable_tuple['fulfillable']; |
57
|
|
|
|
58
|
|
|
if ( ! isset( $relations[ $comparison ] ) ) { |
59
|
|
|
$relations[ $comparison ] = array(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$relations[ $comparison ][] = $this->fulfillable_to_foreign( $fulfillable ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( ! empty( $relations['OR'] ) ) { |
66
|
|
|
$collection['relation'] = 'OR'; |
67
|
|
|
} |
68
|
|
|
foreach ( $relations as $relation => $fulfillables ) { |
69
|
|
|
$collection[] = array( 'relation' => $relation ) + $fulfillables; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ( count( $relations ) === 1 ) { |
73
|
|
|
// we only have one relation group so we simplify the fulfillables with 1 level |
74
|
|
|
$collection = $collection[0]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return array_filter( $collection ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
3 |
|
public function foreign_to_fulfillable( $foreign ) { |
84
|
3 |
|
if ( ! is_array( $foreign ) ) { |
85
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid data passed to array condition translator: ' . print_r( $foreign, true ) ); |
86
|
2 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
if ( isset( $foreign['type'] ) ) { |
90
|
3 |
|
return $this->foreign_to_native_condition( $foreign ); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
return $this->foreign_to_native_fulfillable_collection( $foreign ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Translate a Condition |
98
|
|
|
* |
99
|
|
|
* @param array $foreign |
100
|
|
|
* @return Condition |
101
|
|
|
*/ |
102
|
|
|
protected function foreign_to_native_condition( $foreign ) { |
103
|
|
|
$condition_type = $foreign['type']; |
104
|
|
|
$comparison_operator = isset( $foreign['compare'] ) ? $foreign['compare'] : '='; |
105
|
|
|
$value = isset( $foreign['value'] ) ? $foreign['value'] : ''; |
106
|
|
|
|
107
|
|
|
$condition = $this->condition_factory->make( $condition_type ); |
108
|
|
|
$condition->set_comparison_operator( $comparison_operator ); |
109
|
|
|
$condition->set_value( $value ); |
110
|
|
|
return $condition; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Translate a Fulfillable_Collection |
115
|
|
|
* |
116
|
|
|
* @param array $foreign |
117
|
|
|
* @return Fulfillable_Collection |
118
|
|
|
*/ |
119
|
|
|
protected function foreign_to_native_fulfillable_collection( $foreign ) { |
120
|
|
|
$fulfillable_comparison = isset( $foreign['relation'] ) ? $foreign['relation'] : 'AND'; |
121
|
|
|
$collection = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' ); |
122
|
|
|
foreach ( $foreign as $key => $value ) { |
123
|
|
|
if ( $key === 'relation' ) { |
124
|
|
|
continue; // ignore the relation key - we are only interested in condition definitions |
125
|
|
|
} |
126
|
|
|
$collection->add_fulfillable( $this->foreign_to_fulfillable( $value ), $fulfillable_comparison ); |
127
|
|
|
} |
128
|
|
|
return $collection; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|