1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* InRule |
4
|
|
|
* |
5
|
|
|
* @package php-logical-filter |
6
|
|
|
* @author Jean Claveau |
7
|
|
|
*/ |
8
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class represents a rule that expect a value to belong to a list of others. |
12
|
|
|
*/ |
13
|
|
|
class InRule extends OrRule |
14
|
|
|
{ |
15
|
|
|
use Trait_RuleWithField; |
16
|
|
|
|
17
|
|
|
/** @var string operator */ |
18
|
|
|
const operator = 'in'; |
19
|
|
|
|
20
|
|
|
/** @var array $native_possibilities */ |
21
|
|
|
protected $native_possibilities = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $field The field to apply the rule on. |
25
|
|
|
* @param mixed $possibilities The values the field can belong to. |
26
|
|
|
*/ |
27
|
81 |
|
public function __construct($field, $possibilities, array $options=[]) |
28
|
|
|
{ |
29
|
81 |
|
if (! empty($options)) { |
30
|
53 |
|
$this->setOptions($options); |
31
|
53 |
|
} |
32
|
|
|
|
33
|
81 |
|
$this->field = $field; |
34
|
81 |
|
$this->addPossibilities( $possibilities ); |
35
|
81 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
78 |
|
public function getPossibilities() |
41
|
|
|
{ |
42
|
78 |
|
return array_values($this->native_possibilities); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param mixed possibilities |
47
|
|
|
* |
48
|
|
|
* @return InRule $this |
49
|
|
|
*/ |
50
|
81 |
|
public function addPossibilities($possibilities) |
51
|
|
|
{ |
52
|
81 |
|
if ( is_object($possibilities) |
53
|
81 |
|
&& $possibilities instanceof \IteratorAggregate |
54
|
81 |
|
&& method_exists($possibilities, 'toArray') |
55
|
81 |
|
) { |
56
|
2 |
|
$possibilities = $possibilities->toArray(); |
|
|
|
|
57
|
2 |
|
} |
58
|
|
|
|
59
|
81 |
|
if ( ! is_array($possibilities)) { |
60
|
|
|
$possibilities = [$possibilities]; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
81 |
|
$possibilities = array_map([$this, 'checkOperandAndExtractValue'], $possibilities); |
|
|
|
|
64
|
|
|
|
65
|
|
|
// unique possibilities |
66
|
81 |
|
foreach ($possibilities as &$possibility) { |
67
|
78 |
|
if (is_scalar($possibility)) { |
68
|
78 |
|
$id = hash('crc32b', $possibility); |
69
|
78 |
|
} |
70
|
|
|
else { |
71
|
8 |
|
$id = hash('crc32b', serialize($possibility)); |
72
|
|
|
} |
73
|
|
|
|
74
|
78 |
|
if ( ! isset($this->native_possibilities[ $id ])) { |
75
|
78 |
|
$this->native_possibilities[ $id ] = $possibility; |
76
|
78 |
|
$require_cache_flush = true; |
77
|
78 |
|
} |
78
|
81 |
|
} |
79
|
|
|
|
80
|
81 |
|
if (isset($require_cache_flush)) { |
81
|
78 |
|
$this->flushCache(); |
82
|
78 |
|
} |
83
|
|
|
|
84
|
81 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array possibilities |
89
|
|
|
* |
90
|
|
|
* @return InRule $this |
91
|
|
|
*/ |
92
|
|
|
public function addOperand( AbstractRule $operand ) |
93
|
|
|
{ |
94
|
|
|
$this->addPossibilities([$operand->getValue()]); |
|
|
|
|
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param mixed possibilities |
101
|
|
|
* |
102
|
|
|
* @return InRule $this |
103
|
|
|
*/ |
104
|
17 |
|
public function setPossibilities($possibilities) |
105
|
|
|
{ |
106
|
17 |
|
$this->native_possibilities = []; |
107
|
17 |
|
$this->addPossibilities($possibilities); |
108
|
17 |
|
$this->flushCache(); |
109
|
|
|
|
110
|
17 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array possibilities |
115
|
|
|
* |
116
|
|
|
* @return InRule $this |
117
|
|
|
*/ |
118
|
5 |
|
public function setOperands(array $operands) |
119
|
|
|
{ |
120
|
5 |
|
$this->addPossibilities($operands); |
121
|
|
|
|
122
|
4 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
78 |
|
protected function checkOperandAndExtractValue($operand) |
129
|
|
|
{ |
130
|
78 |
|
if ( ! $operand instanceof AbstractAtomicRule) { |
131
|
78 |
|
return $operand; |
132
|
|
|
} |
133
|
|
|
|
134
|
6 |
|
if ( ! ($operand instanceof EqualRule && $operand->getField() == $this->field) ) { |
135
|
2 |
|
throw new \InvalidArgumentException( |
136
|
|
|
"Trying to set an invalid operand of an InRule: " |
137
|
2 |
|
.var_export($operand, true) |
138
|
2 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
5 |
|
return $operand->getValue(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return InRule $this |
146
|
|
|
*/ |
147
|
25 |
|
public function getOperands() |
148
|
|
|
{ |
149
|
25 |
|
if ( ! empty($this->cache['operands'])) { |
150
|
6 |
|
return $this->cache['operands']; |
151
|
|
|
} |
152
|
|
|
|
153
|
25 |
|
$operands = []; |
154
|
25 |
|
foreach ($this->native_possibilities as $value) { |
155
|
24 |
|
$operands[] = new EqualRule($this->field, $value); |
156
|
25 |
|
} |
157
|
|
|
|
158
|
25 |
|
return $this->cache['operands'] = $operands; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
77 |
|
public function getValues() |
165
|
|
|
{ |
166
|
77 |
|
return $this->getPossibilities(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
77 |
View Code Duplication |
public function toArray(array $options=[]) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$default_options = [ |
177
|
77 |
|
'show_instance' => false, |
178
|
77 |
|
]; |
179
|
77 |
|
foreach ($default_options as $default_option => &$default_value) { |
180
|
77 |
|
if ( ! isset($options[ $default_option ])) { |
181
|
77 |
|
$options[ $default_option ] = $default_value; |
182
|
77 |
|
} |
183
|
77 |
|
} |
184
|
|
|
|
185
|
77 |
|
$class = get_class($this); |
186
|
|
|
|
187
|
77 |
|
if ( ! $options['show_instance'] && isset($this->cache['array'])) { |
188
|
48 |
|
return $this->cache['array']; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$array = [ |
192
|
77 |
|
$this->getField(), |
193
|
77 |
|
$options['show_instance'] ? $this->getInstanceId() : $class::operator, |
194
|
77 |
|
$this->getValues(), |
195
|
77 |
|
]; |
196
|
|
|
|
197
|
77 |
|
if ( ! $options['show_instance']) { |
198
|
77 |
|
return $this->cache['array'] = $array; |
199
|
|
|
} |
200
|
|
|
else { |
201
|
|
|
return $array; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
*/ |
207
|
1 |
|
public function toString(array $options=[]) |
208
|
|
|
{ |
209
|
1 |
|
if (isset($this->cache['string'])) { |
210
|
1 |
|
return $this->cache['string']; |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
$operator = self::operator; |
214
|
|
|
|
215
|
1 |
|
$stringified_possibilities = '[' . implode(', ', array_map(function($possibility) { |
216
|
1 |
|
return var_export($possibility, true); |
217
|
1 |
|
}, $this->getPossibilities()) ) .']'; |
218
|
|
|
|
219
|
1 |
|
return $this->cache['string'] = "['{$this->getField()}', '$operator', $stringified_possibilities]"; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
*/ |
224
|
44 |
|
public function isNormalizationAllowed(array $contextual_options) |
225
|
|
|
{ |
226
|
44 |
|
if (null === ($threshold = $this->getOption('in.normalization_threshold', $contextual_options))) { |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
44 |
|
return count($this->native_possibilities) <= $threshold; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool If the InRule can have a solution or not |
235
|
|
|
*/ |
236
|
3 |
|
public function hasSolution(array $contextual_options=[]) |
237
|
|
|
{ |
238
|
3 |
|
return ! empty($this->getPossibilities()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* There is no negations into an InRule |
243
|
|
|
*/ |
244
|
32 |
|
public function removeNegations(array $contextual_options) |
245
|
|
|
{ |
246
|
32 |
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/**/ |
250
|
|
|
} |
251
|
|
|
|