1 | <?php |
||
10 | class Array_Translator extends Translator { |
||
11 | |||
12 | /** |
||
13 | * Condition factory used to translated condition types |
||
14 | * |
||
15 | * @var Factory |
||
16 | */ |
||
17 | protected $condition_factory; |
||
18 | |||
19 | /** |
||
20 | * Constructor |
||
21 | * |
||
22 | * @param Factory $condition_factory |
||
23 | */ |
||
24 | public function __construct( Factory $condition_factory ) { |
||
27 | |||
28 | /** |
||
29 | * {@inheritDoc} |
||
30 | */ |
||
31 | protected function condition_to_foreign( Condition $condition ) { |
||
32 | return array( |
||
33 | 'type' => $this->condition_factory->get_type( get_class( $condition ) ), |
||
34 | 'compare' => $condition->get_comparison_operator(), |
||
35 | 'value' => $condition->get_value(), |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritDoc} |
||
41 | */ |
||
42 | protected function fulfillable_collection_to_foreign( Fulfillable_Collection $fulfillable_collection ) { |
||
43 | $fulfillables = $fulfillable_collection->get_fulfillables(); |
||
44 | if ( empty( $fulfillables ) ) { |
||
45 | return array(); |
||
46 | } |
||
47 | |||
48 | $collection = array( |
||
49 | 'relation' => 'AND', |
||
50 | ); |
||
51 | |||
52 | $relations = array(); |
||
53 | foreach ( $fulfillables as $fulfillable_tuple ) { |
||
54 | $comparison = $fulfillable_tuple['fulfillable_comparison']; |
||
55 | $fulfillable = $fulfillable_tuple['fulfillable']; |
||
56 | |||
57 | if ( ! isset( $relations[ $comparison ] ) ) { |
||
58 | $relations[ $comparison ] = array(); |
||
59 | } |
||
60 | |||
61 | $relations[ $comparison ][] = $this->fulfillable_to_foreign( $fulfillable ); |
||
62 | } |
||
63 | |||
64 | if ( ! empty( $relations['OR'] ) ) { |
||
65 | $collection['relation'] = 'OR'; |
||
66 | } |
||
67 | foreach ( $relations as $relation => $fulfillables ) { |
||
68 | $collection[] = array( 'relation' => $relation ) + $fulfillables; |
||
69 | } |
||
70 | |||
71 | if ( count( $relations ) === 1 ) { |
||
72 | // we only have one relation group so we simplify the fulfillables with 1 level |
||
73 | $collection = $collection[0]; |
||
74 | } |
||
75 | |||
76 | return array_filter( $collection ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritDoc} |
||
81 | */ |
||
82 | 3 | public function foreign_to_fulfillable( $foreign ) { |
|
94 | |||
95 | /** |
||
96 | * Translate a Condition |
||
97 | * |
||
98 | * @param array $foreign |
||
99 | * @return Condition |
||
100 | */ |
||
101 | protected function foreign_to_native_condition( $foreign ) { |
||
111 | |||
112 | /** |
||
113 | * Translate a Fulfillable_Collection |
||
114 | * |
||
115 | * @param array $foreign |
||
116 | * @return Fulfillable_Collection |
||
117 | */ |
||
118 | protected function foreign_to_native_fulfillable_collection( $foreign ) { |
||
129 | } |
||
130 |