1 | <?php |
||
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 ) { |
||
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 ) { |
||
139 | |||
140 | /** |
||
141 | * Make conditions friendly for frontend. |
||
142 | * |
||
143 | * @param array $foreign |
||
144 | * @return array |
||
145 | */ |
||
146 | public function foreign_to_json( $foreign ) { |
||
173 | } |
||
174 |