Comparison.php
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace ACP\Search;
use ACP\Search\Query\Bindings;
use LogicException;
abstract class Comparison {
/**
* @var Operators
*/
protected $operators;
/**
* @var string
*/
protected $value_type;
/**
* @var Labels
*/
protected $labels;
/**
* @param Operators $operators
* @param string $value_type
* @param Labels $labels
*/
public function __construct( Operators $operators, $value_type = null, Labels $labels = null ) {
if ( null === $labels ) {
$labels = new Labels();
}
if ( null === $value_type ) {
$value_type = Value::STRING;
}
$this->labels = $labels;
$this->value_type = $value_type;
$this->operators = $operators;
$this->validate_value_type();
}
private function validate_value_type() {
$value_types = [
Value::DATE,
Value::INT,
Value::DECIMAL,
Value::STRING,
];
if ( ! in_array( $this->value_type, $value_types ) ) {
throw new LogicException( 'Unsupported value type found.' );
}
}
/**
* @return Operators
*/
public function get_operators() {
return $this->operators;
}
/**
* @return string
*/
public function get_value_type() {
return $this->value_type;
}
/**
* @return array
*/
public function get_labels() {
$labels = [];
foreach ( $this->get_operators() as $operator ) {
$labels[ $operator ] = $this->labels->get_offset( $operator );
}
return $labels;
}
/**
* @param string $operator
* @param Value $value
*
* @return Bindings
*/
final public function get_query_bindings( $operator, Value $value ) {
if ( $this->operators->search( $operator ) === false ) {
throw new LogicException( 'Unsupported operator found.' );
}
if ( $this->value_type !== $value->get_type() ) {
throw new LogicException( 'Value types are not identical.' );
}
return $this->create_query_bindings( $operator, $value );
}
/**
* @param string $operator
* @param Value $value
*
* @return Bindings
*/
abstract protected function create_query_bindings( $operator, Value $value );
}