|
1
|
|
|
<?php |
|
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* a >= x |
|
6
|
|
|
*/ |
|
7
|
|
|
class AboveOrEqualRule extends OrRule |
|
8
|
|
|
{ |
|
9
|
|
|
use Trait_RuleWithField; |
|
10
|
|
|
|
|
11
|
|
|
/** @var string operator */ |
|
12
|
|
|
const operator = '>='; |
|
13
|
|
|
|
|
14
|
|
|
/** @var mixed minimum */ |
|
15
|
|
|
protected $minimum; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $field The field to apply the rule on. |
|
19
|
|
|
* @param array $value The value the field can below to. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($field, $minimum, array $options=[]) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!empty($options)) { |
|
24
|
|
|
$this->setOptions($options); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->field = $field; |
|
28
|
|
|
$this->minimum = $minimum; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return mixed The minimum for the field of this rule |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getMinimum() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->minimum; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Defines the minimum of the current rule |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $minimum |
|
43
|
|
|
* @return BelowOrEqualRule $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setMinimum($minimum) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($minimum === null) { |
|
48
|
|
|
throw new \InvalidArgumentException( |
|
49
|
|
|
"The minimum of a below or equal rule cannot be null" |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($this->minimum == $minimum) |
|
54
|
|
|
return $this; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$this->minimum = $minimum; |
|
57
|
|
|
$this->flushCache(); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isNormalizationAllowed(array $contextual_options=[]) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getOption('above_or_equal.normalization', $contextual_options); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array $operands |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getOperands() |
|
74
|
|
|
{ |
|
75
|
|
|
if (! empty($this->cache['operands'])) { |
|
76
|
|
|
return $this->cache['operands']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$operands = [ |
|
80
|
|
|
new AboveRule($this->field, $this->minimum), |
|
81
|
|
|
new EqualRule($this->field, $this->minimum), |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
return $this->cache['operands'] = $operands; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the minimum and the field of the current instance by giving |
|
89
|
|
|
* an array of opereands as parameter. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $operands |
|
92
|
|
|
* @return BelowOrEqualRule $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setOperands(array $operands) |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($operands as $operand) { |
|
97
|
|
|
if ($operand instanceof EqualRule) { |
|
98
|
|
|
$equalRuleField = $operand->getField(); |
|
99
|
|
|
$equalRuleValue = $operand->getValue(); |
|
100
|
|
|
} |
|
101
|
|
|
elseif ($operand instanceof AboveRule) { |
|
102
|
|
|
$aboveRuleField = $operand->getField(); |
|
103
|
|
|
$aboveRuleValue = $operand->getLowerLimit(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ( count($operands) != 2 |
|
108
|
|
|
|| !isset($equalRuleValue) |
|
109
|
|
|
|| !isset($aboveRuleValue) |
|
110
|
|
|
|| $aboveRuleValue != $equalRuleValue |
|
111
|
|
|
|| $aboveRuleField != $equalRuleField |
|
|
|
|
|
|
112
|
|
|
) { |
|
113
|
|
|
throw new \InvalidArgumentException( |
|
114
|
|
|
"Operands must be an array of two rules like (field > minimum || field = minimum) instead of:\n" |
|
115
|
|
|
.var_export($operands, true) |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->setMinimum($aboveRuleValue); |
|
120
|
|
|
$this->setField($aboveRuleField); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getValues() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getMinimum(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public function toArray(array $options=[]) |
|
139
|
|
|
{ |
|
140
|
|
|
$default_options = [ |
|
141
|
|
|
'show_instance' => false, |
|
142
|
|
|
]; |
|
143
|
|
|
foreach ($default_options as $default_option => &$default_value) { |
|
144
|
|
|
if (!isset($options[ $default_option ])) |
|
145
|
|
|
$options[ $default_option ] = $default_value; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
try { |
|
149
|
|
|
// TODO replace this rule by a simple OrRule? |
|
150
|
|
|
return [ |
|
151
|
|
|
$this->getField(), |
|
152
|
|
|
$options['show_instance'] ? $this->getInstanceId() : self::operator, |
|
153
|
|
|
$this->getValues(), |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
catch (\RuntimeException $e) { |
|
157
|
|
|
return parent::toArray(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
*/ |
|
163
|
|
|
public function toString(array $options=[]) |
|
164
|
|
|
{ |
|
165
|
|
|
if (isset($this->cache['string'])) |
|
166
|
|
|
return $this->cache['string']; |
|
167
|
|
|
|
|
168
|
|
|
$operator = self::operator; |
|
169
|
|
|
|
|
170
|
|
|
return $this->cache['string'] = "['{$this->getField()}', '$operator', " . var_export($this->getValues(), true). "]"; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/**/ |
|
174
|
|
|
} |
|
175
|
|
|
|