1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container\Condition; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Container\Fulfillable\Fulfillable; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base container condition class |
10
|
|
|
*/ |
11
|
|
|
abstract class Condition implements Fulfillable { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Condition value to check |
15
|
|
|
* |
16
|
|
|
* @var mixed |
17
|
|
|
*/ |
18
|
|
|
protected $value; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Comparers to use for condition checking |
22
|
|
|
* |
23
|
|
|
* @var array<Comparer> |
24
|
|
|
*/ |
25
|
|
|
protected $comparers = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Comparison string to use |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $comparison_operator = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the condition value |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function get_value() { |
40
|
|
|
if ( $this->get_comparison_operator() !== 'CUSTOM' && is_callable( $this->value ) ) { |
41
|
|
|
return call_user_func( $this->value ); |
42
|
|
|
} |
43
|
|
|
return $this->value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the condition value |
48
|
|
|
* |
49
|
|
|
* @param mixed $value |
50
|
|
|
* @return Condition $this |
51
|
|
|
*/ |
52
|
|
|
public function set_value( $value ) { |
53
|
|
|
$this->value = $value; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the condition comparers |
59
|
|
|
* |
60
|
|
|
* @return array<Comparer> |
61
|
|
|
*/ |
62
|
|
|
protected function get_comparers() { |
63
|
|
|
return $this->comparers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the condition comparers |
68
|
|
|
* |
69
|
|
|
* @param array<Comparer> $comparers |
70
|
|
|
* @return Condition $this |
71
|
|
|
*/ |
72
|
|
|
public function set_comparers( $comparers ) { |
73
|
|
|
$this->comparers = $comparers; |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Find the first operator which supports $comparison_operator and check if it is correct for $a and $b |
79
|
|
|
* |
80
|
|
|
* @param mixed $a |
81
|
|
|
* @param string $comparison_operator |
82
|
|
|
* @param mixed $b |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
protected function compare( $a, $comparison_operator, $b ) { |
|
|
|
|
86
|
|
|
$comparers = $this->get_comparers(); |
87
|
|
|
foreach ( $comparers as $comparer ) { |
88
|
|
|
if ( ! $comparer->supports_comparison_operator( $comparison_operator ) ) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
return $comparer->is_correct( $a, $comparison_operator, $b ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
Incorrect_Syntax_Exception::raise( 'Unsupported container condition comparison operator used: ' . $comparison_operator ); |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get comparison sign used |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function get_comparison_operator() { |
104
|
|
|
return $this->comparison_operator; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set comparison sign |
109
|
|
|
* |
110
|
|
|
* @param string $comparison_operator |
111
|
|
|
* @return Comparer $this |
112
|
|
|
*/ |
113
|
|
|
public function set_comparison_operator( $comparison_operator ) { |
114
|
|
|
$this->comparison_operator = $comparison_operator; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
} |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.