Completed
Push — milestone/2_0/container-condit... ( af96ef...c41f81 )
by
unknown
03:01
created

Condition::set_comparison_operator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
6
7
/**
8
 * Base container condition class
9
 */
10
abstract class Condition implements Fulfillable {
11
12
	/**
13
	 * Condition value to check
14
	 * 
15
	 * @var mixed
16
	 */
17
	protected $value;
18
19
	/**
20
	 * Comparers to use for condition checking
21
	 * 
22
	 * @var array<Comparer>
23
	 */
24
	protected $comparers = array();
25
26
	/**
27
	 * Comparison string to use
28
	 * 
29
	 * @var string
30
	 */
31
	protected $comparison_operator = '';
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param  array<Carbon_Fields\Container\Condition\Comparer\Comparer> $comparers
37
	 */
38
	public function __construct( $comparers ) {
39
		$this->set_comparers( $comparers );
40
	}
41
	
42
	/**
43
	 * Get the condition value
44
	 * 
45
	 * @return mixed
46
	 */
47
	public function get_value() {
48
		return $this->value;
49
	}
50
	
51
	/**
52
	 * Set the condition value
53
	 *
54
	 * @param mixed $value
55
	 * @return Condition $this
56
	 */
57
	public function set_value( $value ) {
58
		$this->value = $value;
59
		return $this;
60
	}
61
	
62
	/**
63
	 * Get the condition comparers
64
	 * 
65
	 * @return array<Comparer>
66
	 */
67
	protected function get_comparers() {
68
		return $this->comparers;
69
	}
70
	
71
	/**
72
	 * Set the condition comparers
73
	 *
74
	 * @param array<Comparer> $comparers
75
	 * @return Condition $this
76
	 */
77
	protected function set_comparers( $comparers ) {
78
		$this->comparers = $comparers;
79
		return $this;
80
	}
81
	
82
	/**
83
	 * Check if any comparer is correct for $a and $b
84
	 *
85
	 * @param mixed  $a
86
	 * @param string $comparison_operator
87
	 * @param mixed  $b
88
	 * @return bool
89
	 */
90
	protected function first_supported_comparer_is_correct( $a, $comparison_operator, $b ) {
91
		$comparers = $this->get_comparers();
92
		foreach ( $comparers as $comparer ) {
93
			if ( ! $comparer->supports_comparison_operator( $comparison_operator ) ) {
94
				continue;
95
			}
96
			return $comparer->is_correct( $a, $comparison_operator, $b );
97
		}
98
99
		Incorrect_Syntax_Exception::raise( 'Unsupported container condition comparison operator used: ' . $comparison_operator );
100
		return false;
101
	}
102
103
	/**
104
	 * Get comparison sign used
105
	 * 
106
	 * @return string
107
	 */
108
	public function get_comparison_operator() {
109
		return $this->comparison_operator;
110
	}
111
112
	/**
113
	 * Set comparison sign
114
	 * 
115
	 * @param string $comparison_operator
116
	 * @return Comparer $this
117
	 */
118
	public function set_comparison_operator( $comparison_operator ) {
119
		$this->comparison_operator = $comparison_operator;
120
		return $this;
121
	}
122
}