Completed
Push — master ( 77feef...2e66fa )
by
unknown
08:23 queued 11s
created

Array_Translator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
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_Collection;
7
use Carbon_Fields\Container\Condition\Condition;
8
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
9
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 ) {
25
		$this->condition_factory = $condition_factory;
26
	}
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 ) {
83 3
		if ( ! is_array( $foreign ) ) {
84
			Incorrect_Syntax_Exception::raise( 'Invalid data passed to array condition translator: ' . print_r( $foreign, true ) );
85
			return null;
86
		}
87
88 3
		if ( isset( $foreign['type'] ) ) {
89 3
			return $this->foreign_to_native_condition( $foreign );
90
		}
91
92 2
		return $this->foreign_to_native_fulfillable_collection( $foreign );
93
	}
94
95
	/**
96
	 * Translate a Condition
97
	 *
98
	 * @param  array     $foreign
99
	 * @return Condition
100
	 */
101
	protected function foreign_to_native_condition( $foreign ) {
102
		$condition_type = $foreign['type'];
103
		$comparison_operator = isset( $foreign['compare'] ) ? $foreign['compare'] : '=';
104
		$value = isset( $foreign['value'] ) ? $foreign['value'] : '';
105
106
		$condition = $this->condition_factory->make( $condition_type );
107
		$condition->set_comparison_operator( $comparison_operator );
108
		$condition->set_value( $value );
109
		return $condition;
110
	}
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 ) {
119
		$fulfillable_comparison = isset( $foreign['relation'] ) ? $foreign['relation'] : 'AND';
120
		$collection = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
121
		foreach ( $foreign as $key => $value ) {
122
			if ( $key === 'relation' ) {
123
				continue; // ignore the relation key - we are only interested in condition definitions
124
			}
125
			$collection->add_fulfillable( $this->foreign_to_fulfillable( $value ), $fulfillable_comparison );
126
		}
127
		return $collection;
128
	}
129
}
130